java - Problems while displaying path between two points in google maps -
this problem may sound little weird, whenever run app crashes , when change name of latlang variable in function private latlng getcurrentlocation(), starts working. when change anything(even in other file) crashes , have keep changing variable names keep running. sure of problem, related latlang variable.
getapproxtimemaps.java
public class getapproxtimemaps extends fragmentactivity { private static double destilatitude; private static double destilongitude; googlemap mmap; gmapv2direction md; latlng userlocation; latlng destination; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_get_approx_time_maps); if (android.os.build.version.sdk_int > 9) { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); } userlocation = getcurrentlocation(); double lati = userlocation.latitude; double longi = userlocation.longitude; jsonobject jasonobject = getlocationinfo("infosys"); boolean value = getlatlong(jasonobject); system.out.println("value is: "+ value); destination = new latlng(destilatitude, destilongitude); system.out.println("destination latitude :" + destilatitude); system.out.println("destination longitude :" + destilongitude); md = new gmapv2direction(); mmap = ((supportmapfragment)getsupportfragmentmanager() .findfragmentbyid(r.id.map)).getmap(); double tozoomlat = (destination.latitude + userlocation.latitude)/2; double tozoomlon = (destination.longitude + userlocation.longitude)/2; latlng coordinates = new latlng(tozoomlat, tozoomlon); mmap.animatecamera(cameraupdatefactory.newlatlngzoom(coordinates, 12)); mmap.addmarker(new markeroptions().position(userlocation).title("start")); mmap.addmarker(new markeroptions().position(destination).title("end")); document doc = md.getdocument(userlocation, destination, gmapv2direction.mode_driving); int duration = md.getdurationvalue(doc); string distance = md.getdistancetext(doc); string start_address = md.getstartaddress(doc); string copy_right = md.getcopyrights(doc); system.out.println(duration); arraylist<latlng> directionpoint = md.getdirection(doc); polylineoptions rectline = new polylineoptions().width(3).color(color.blue); for(int = 0 ; < directionpoint.size() ; i++) { rectline.add(directionpoint.get(i)); } mmap.addpolyline(rectline); } private latlng getcurrentlocation(){ locationmanager service = (locationmanager) getsystemservice(location_service); criteria criteria = new criteria(); string provider = service.getbestprovider(criteria, false); location location = service.getlastknownlocation(provider); latlng userlocat = new latlng(location.getlatitude(),location.getlongitude()); return userlocat; } public static jsonobject getlocationinfo(string address) { stringbuilder stringbuilder = new stringbuilder(); try { address = address.replaceall(" ","%20"); httppost httppost = new httppost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"); httpclient client = new defaulthttpclient(); httpresponse response; stringbuilder = new stringbuilder(); response = client.execute(httppost); httpentity entity = response.getentity(); inputstream stream = entity.getcontent(); int b; while ((b = stream.read()) != -1) { stringbuilder.append((char) b); } } catch (clientprotocolexception e) { } catch (ioexception e) { } jsonobject jsonobject = new jsonobject(); try { jsonobject = new jsonobject(stringbuilder.tostring()); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } return jsonobject; } public static boolean getlatlong(jsonobject jsonobject) { try { destilongitude = ((jsonarray)jsonobject.get("results")).getjsonobject(0) .getjsonobject("geometry").getjsonobject("location") .getdouble("lng"); destilatitude = ((jsonarray)jsonobject.get("results")).getjsonobject(0) .getjsonobject("geometry").getjsonobject("location") .getdouble("lat"); } catch (jsonexception e) { return false; } return true; }