How can I calculate the distance between 2 points on Google Maps V3

1

Good afternoon.

I am using the Google Maps API which I must trace from any location and trace a line to my geolocation.

This I have already accomplished, but the problem is that when I perform the calculation using SphericalUtilities.computerdistanceBetween O Location.distanceTo() it gives me an amount that does not come close to the correct or real distance.

For example: 1.2112E7 instead of 1,200m

Code (DistanceTo):

Location location = new Location("A");
                location.setLatitude(location.getLatitude());
                location.setLongitude(location.getLongitude());


                Location locationb = new Location("B");
                locationb.setLatitude(latLng.latitude);
                locationb.setLongitude(latLng.longitude);

                location.distanceTo(locationb);
                String distancia = locationb.toString();
                Toast.makeText(MapsActivity.this, distancia, Toast.LENGTH_SHORT).show();

Code (SphericalUtil):

Double distancia = SphericalUtil.computeDistanceBetween(latLng,latLng);
            String distanciaC = distancia.toString();
            Toast.makeText(MapsActivity.this, distanciaC, Toast.LENGTH_SHORT).show();

I know what the code does is that it turns the world around and gets a greater distance, but I do not know how to correct it

    
asked by QuickAccount456 11.11.2017 в 22:36
source

2 answers

1

This should be something like this:

    Location locationA = new Location("punto A");

    locationA.setLatitude(latA);
    locationA.setLongitude(lngA);

    Location locationB = new Location("punto B");

    locationB.setLatitude(latB);
    locationB.setLongitude(lngB);

    float distance = locationA.distanceTo(locationB);

The value it returns is in meters.

Now you can try this function in Kilometers CalculationByDistance :

public double CalculationByDistance(LatLng StartP, LatLng EndP) {
        int Radius = 6371;// radio de la tierra en  kilómetros
        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));
        double valueResult = Radius * c;
        double km = valueResult / 1;
        DecimalFormat newFormat = new DecimalFormat("####");
        int kmInDec = Integer.valueOf(newFormat.format(km));
        double meter = valueResult % 1000;
        int meterInDec = Integer.valueOf(newFormat.format(meter));
        Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
                + " Meter   " + meterInDec);

        return Radius * c;
    }

Keep in mind that in meters the number will be larger.

    
answered by 11.11.2017 в 23:23
1

If you use the Location object, you have the function distanceBetween

Its use:

float[] results = new float[1];
Location.distanceBetween(pointA.latitude, pointA.longitude, pointB.latitude, pointB.longitude, results);

The result returns it in meters.

Extracted from SO not tested ...

    
answered by 13.11.2017 в 12:07