Android Maps v2 API...How to calculate distance between two locations (Lat, long) -
in "native" android app i'm trying calculate distance in feet and/or meters (as crow flies) between 2 locations on map. ideally there method take 2 latlng values (as have readily available) input , return distance in meters and/or yards. noted above i'm using android maps v2 api.
i've looked @ many postings (20-30) regarding calculating distances , haven't found have helped me resolve issue. again native android application , i'm using maps v2 api.
not sure if best approach i'm trying use location
method:
distancebetween(double startlatitude, double startlongitude, double endlatitude, double endlongitude, float[] results)
but unfortunately app crashes every time because null value results. have verified via debugger latitude , longitude dummy values i'm passing in valid , value of results
null (hence problem). here logcat:
04-04 23:48:04.770: d/onmapclick(17970): lat/lng: (32.90843038503306,-117.22537234425546) 04-04 23:48:08.710: d/dalvikvm(17970): threadid=1: still suspended after undo (sc=1 dc=1 s=y) 04-04 23:48:08.710: d/dalvikvm(17970): gc_for_malloc freed 7010 objects / 641712 bytes in 121ms 04-04 23:48:24.980: d/androidruntime(17970): shutting down vm 04-04 23:48:24.980: w/dalvikvm(17970): threadid=1: thread exiting uncaught exception (group=0x40020950) 04-04 23:48:25.100: e/androidruntime(17970): fatal exception: main 04-04 23:48:25.100: e/androidruntime(17970): java.lang.illegalargumentexception: results null or has length < 1 04-04 23:48:25.100: e/androidruntime(17970): @ android.location.location.distancebetween(location.java:394) 04-04 23:48:25.100: e/androidruntime(17970): @ com.example.googleplacesandmaps.placesmapactivity$1.onmapclick(placesmapactivity.java:259) 04-04 23:48:25.100: e/androidruntime(17970): @ com.google.android.gms.maps.googlemap$6.onmapclick(unknown source) 04-04 23:48:25.100: e/androidruntime(17970): @ com.google.android.gms.internal.t$a.ontransact(unknown source) 04-04 23:48:25.100: e/androidruntime(17970): @ android.os.binder.transact(binder.java:249) 04-04 23:48:25.100: e/androidruntime(17970): @ com.google.android.gms.maps.internal.ionmapclicklistener$stub$proxy.onmapclick(ionmapclicklistener.java:93) 04-04 23:48:25.100: e/androidruntime(17970): @ maps.i.s.b(unknown source) 04-04 23:48:25.100: e/androidruntime(17970): @ maps.y.v.c(unknown source) 04-04 23:48:25.100: e/androidruntime(17970): @ maps.y.bf.onsingletapconfirmed(unknown source) 04-04 23:48:25.100: e/androidruntime(17970): @ maps.d.v.onsingletapconfirmed(unknown source) 04-04 23:48:25.100: e/androidruntime(17970): @ maps.d.j.handlemessage(unknown source) 04-04 23:48:25.100: e/androidruntime(17970): @ android.os.handler.dispatchmessage(handler.java:99) 04-04 23:48:25.100: e/androidruntime(17970): @ android.os.looper.loop(looper.java:123) 04-04 23:48:25.100: e/androidruntime(17970): @ android.app.activitythread.main(activitythread.java:4627) 04-04 23:48:25.100: e/androidruntime(17970): @ java.lang.reflect.method.invokenative(native method) 04-04 23:48:25.100: e/androidruntime(17970): @ java.lang.reflect.method.invoke(method.java:521) 04-04 23:48:25.100: e/androidruntime(17970): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:893) 04-04 23:48:25.100: e/androidruntime(17970): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:651) 04-04 23:48:25.100: e/androidruntime(17970): @ dalvik.system.nativestart.main(native method)
here method i'm using try , calculate distance:
location.distancebetween(double.parsedouble(user_latitude), double.parsedouble(user_longitude), fxdlatitude, fxdlongitude, results);
the results
defined class variable: private float[] results = null;
not sure i'm doing wrong here.
again i'd rather have method take 2 latlng's input parameters because both readily available...but perhaps more importantly have 1 of locations available separate longitude, latitude values...the second location available latlng value because via onmapclick (when user taps screen). use method distancebetween
i'd need someway convert latlng value latitude/longitude pair able use it. know how that?
hopefully there simple way of calculating distances , simple way "extract" latitude/longitude latlng value.
what making location object 2 ?
private float distancebetween(latlng latlng1, latlng latlng2) { location loc1 = new location(locationmanager.gps_provider); location loc2 = new location(locationmanager.gps_provider); loc1.setlatitude(latlng1.latitude); loc1.setlongitude(latlng1.longitude); loc2.setlatitude(latlng2.latitude); loc2.setlongitude(latlng2.longitude); return loc1.distanceto(loc2); }
Comments
Post a Comment