Android Maps v2 API ... How to calculate the distance between two locations (Lat, long)

In my native Android app, I am trying to calculate the distance in feet and / or meters (like a crow) between two locations on a map. Ideally, there would be a method that takes two LatLng values ​​(since this is what I have) as input and returns the distance in meters and / or yards. As noted above, I use the Android Maps v2 API.

I looked through a lot of messages (20-30) regarding distance calculation and did not find any that helped me solve my problem. This is again a native Android app, and I'm using the Maps v2 API.

Not sure if this is the best approach, but I'm currently trying to use a method Location:

distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

But, unfortunately, the application crashes every time because I get a zero value for the results. I checked through the debugger that the valid length and longitude values ​​that I pass are valid and that the value resultsis null (hence the problem). Here is the 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 with 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 is null or has length < 1
04-04 23:48:25.100: E/AndroidRuntime(17970):    at android.location.Location.distanceBetween(Location.java:394)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at com.example.googleplacesandmaps.PlacesMapActivity$1.onMapClick(PlacesMapActivity.java:259)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at com.google.android.gms.maps.GoogleMap$6.onMapClick(Unknown Source)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at com.google.android.gms.internal.t$a.onTransact(Unknown Source)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at android.os.Binder.transact(Binder.java:249)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at com.google.android.gms.maps.internal.IOnMapClickListener$Stub$Proxy.onMapClick(IOnMapClickListener.java:93)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at maps.i.s.b(Unknown Source)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at maps.y.v.c(Unknown Source)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at maps.y.bf.onSingleTapConfirmed(Unknown Source)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at maps.d.v.onSingleTapConfirmed(Unknown Source)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at maps.d.j.handleMessage(Unknown Source)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at android.os.Looper.loop(Looper.java:123)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at java.lang.reflect.Method.invokeNative(Native Method)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at java.lang.reflect.Method.invoke(Method.java:521)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
04-04 23:48:25.100: E/AndroidRuntime(17970):    at dalvik.system.NativeStart.main(Native Method)

Here is the method I'm using now to try to calculate the distance:

            Location.distanceBetween(Double.parseDouble(user_latitude), Double.parseDouble(user_longitude),
                fxdLatitude, fxdLongitude, results);

results defined as a class variable: private float[] results = null;

Not sure what I'm doing wrong here.

, , LatLng , ... , , , , , ... LatLng, onMapClick ( ). distanceBetween - LatLng /, . - , ?

, "" / LatLng.

+5
5

Location ?

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);
    }
+11

, LatLng . , , LatLng .

public static double distance(LatLng StartP, LatLng EndP) {
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2-lat1);
    double dLon = Math.toRadians(lon2-lon1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
    Math.sin(dLon/2) * Math.sin(dLon/2);
    double c = 2 * Math.asin(Math.sqrt(a));
    return 6366000 * c;
}
+3

private float[] results = null; .

private float[] results = new float[1]; results[0].

+1

, , MaciejGórski .

[0]. 2 , [1]. 3 , [2].

0

Kotlin :

LatLng Location:

fun LatLng.toLocation() = Location(LocationManager.GPS_PROVIDER).also {
    it.latitude = latitude
    it.longitude = longitude
}

LatLng Location#distanceTo(...):

val distance = coords1.toLocation().distanceTo(coords2.toLocation())

Define an extension method for LatLngBounds:

fun LatLngBounds.computeDistance(): Float {
    return northeast.toLocation().distanceTo(southwest.toLocation())
}
0
source

All Articles