Find the distance between two locations in android

2019-09-04 08:42发布

问题:

Possible Duplicate:
Distance calculation from my location to destination location in android

I am very new to android google maps i want display distance between two places .for that i write the two autocompletedtextview i get the from and to Locations .by using the

public String host = "https://maps.googleapis.com";
public String searchPath = "/maps/api/place/autocomplete/json";
public String detailsPath = "/maps/api/place/details/json";

I got the places names as string i want calculate the distance between two places using that two string places how can i achieve it Thanks in Advance.

回答1:

i think this code usefull for you :

double distance

Location locationA = new Location(“point A”)

locationA.setLatitude(latA)

locationA.setLongitude(lngA)

Location locationB = new Location(“point B”)

locationB.setLatitude(latB)

LocationB.setLongitude(lngB)

distance = locationA.distanceTo(locationB)


回答2:

To find Distance Try this just pass start and end point return you distance

 double distance;
 GeoPoint StartP;
 GeoPoint EndP;

    distance=CalculationByDistance(StartP,EndP);

    public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
            double Radius = 6371;
            double lat1 = StartP.getLatitudeE6() / 1E6;
            double lat2 = EndP.getLatitudeE6() / 1E6;
            double lon1 = StartP.getLongitudeE6() / 1E6;
            double lon2 = EndP.getLongitudeE6() / 1E6;
            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.atan2(Math.sqrt(a), Math.sqrt(1 - a));
            double temp = Radius * c;
            temp=temp*0.621;
            return temp;
        }