android - Get the nearest location to user from Json -
i'm new android, i'm creating app nearest geocaches device's current location..
i have bit of code :
public class mainactivity extends fragmentactivity { private googlemap map; textview test; private arraylist<latlng> markers = new arraylist<latlng>(); // main code. @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); strictmode.threadpolicy policy = new strictmode.threadpolicy.builder() .permitall().build(); strictmode.setthreadpolicy(policy); test = (textview) findviewbyid(r.id.test); string result = json .getresult("http://www.student.soc.napier.ac.uk/~40053830/cms/markers.php"); map = ((supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map)).getmap(); map.setmylocationenabled(true); try { jsonarray jarray = new jsonarray(result); (int j = 0; j < jarray.length(); j++) { jsonobject json_data = jarray.getjsonobject(j); latlng tmp = new latlng(double.parsedouble(json_data .getstring("latitude")), double.parsedouble(json_data .getstring("longitude"))); map.addmarker(new markeroptions().position(tmp).title( json_data.getstring("name"))); markers.add(tmp); } } catch (jsonexception e) { log.e("log_tag", "error parsing data " + e.tostring()); } location mylocation = map.getmylocation(); location nearest = new location("nearest"); nearest.setlatitude(markers.get(0).latitude); nearest.setlongitude(markers.get(0).longitude); float distance = mylocation.distanceto(nearest); (int = 1; < markers.size(); i++) { location target = new location("target"); target.setlatitude(markers.get(i).latitude); target.setlongitude(markers.get(i).longitude); if (mylocation.distanceto(target) < distance) { nearest = target; distance = mylocation.distanceto(target); } } test.settext((string.format("%.02f", distance * 0.000621371192))); }
i can't seem distance nearest on textview..
here's logcat :
08-18 19:01:50.463: e/androidruntime(16421): fatal exception: main 08-18 19:01:50.463: e/androidruntime(16421): process: com.finlaysmith.motorcycleparking, pid: 16421 08-18 19:01:50.463: e/androidruntime(16421): java.lang.runtimeexception: unable start activity componentinfo{com.finlaysmith.motorcycleparking/com.finlaysmith.motorcycleparking.mainactivity}: java.lang.nullpointerexception 08-18 19:01:50.463: e/androidruntime(16421): @ android.app.activitythread.performlaunchactivity(activitythread.java:2184) 08-18 19:01:50.463: e/androidruntime(16421): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2233) 08-18 19:01:50.463: e/androidruntime(16421): @ android.app.activitythread.access$800(activitythread.java:135) 08-18 19:01:50.463: e/androidruntime(16421): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 08-18 19:01:50.463: e/androidruntime(16421): @ android.os.handler.dispatchmessage(handler.java:102) 08-18 19:01:50.463: e/androidruntime(16421): @ android.os.looper.loop(looper.java:136) 08-18 19:01:50.463: e/androidruntime(16421): @ android.app.activitythread.main(activitythread.java:5001) 08-18 19:01:50.463: e/androidruntime(16421): @ java.lang.reflect.method.invokenative(native method) 08-18 19:01:50.463: e/androidruntime(16421): @ java.lang.reflect.method.invoke(method.java:515) 08-18 19:01:50.463: e/androidruntime(16421): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) 08-18 19:01:50.463: e/androidruntime(16421): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) 08-18 19:01:50.463: e/androidruntime(16421): @ dalvik.system.nativestart.main(native method) 08-18 19:01:50.463: e/androidruntime(16421): caused by: java.lang.nullpointerexception 08-18 19:01:50.463: e/androidruntime(16421): @ com.finlaysmith.motorcycleparking.mainactivity.oncreate(mainactivity.java:66) 08-18 19:01:50.463: e/androidruntime(16421): @ android.app.activity.performcreate(activity.java:5231) 08-18 19:01:50.463: e/androidruntime(16421): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 08-18 19:01:50.463: e/androidruntime(16421): @ android.app.activitythread.performlaunchactivity(activitythread.java:2148)
i know it's " float distance.. can't find how resolve it..
thanks
map.mylocation returning null because hasn't found location yet. may take time. need check null , not part of code if return null (you can later, once has location).
Comments
Post a Comment