Distance between a point and several locations

2019-09-07 16:04发布

问题:

I am developing an application that shows the distance between the user and multiple locations (such as foursquare in the image below) on android, I am using eclipse and would like to know how to calculate the distance between various points. Thank you!

Image: http://i.stack.imgur.com/rsWmO.jpg

回答1:

There are probably many ways to get this done, here's an option.

You could use the distanceTo() method. If you want more than one distance, simply use a loop to repeat it until you've calculated the distances between all the Locations you have at hand.



回答2:

If you're using Google Maps, you can use a Distance Matrix https://developers.google.com/maps/documentation/distancematrix/ https://developers.google.com/maps/documentation/javascript/distancematrix



回答3:

Here I am providing you some sample code for Distance calculation. I have done like this in my project. Here distanceTo() method will return you the distance in double.

private Location currentLocation, distanceLocation;
double distance = 0;

//set your current location 
currentLocation.setLatitude(currentLat);
currentLocation.setLongitude(currentLong);

//set your destination location
distanceLocation = new Location("");
distanceLocation.setLatitude(destinatioLat);
distanceLocation.setLongitude(destinationLong);

distance = currentLocation.distanceTo(distanceLocation)/1000;

Same for more then one location you can use Array for storing distance. Its on you how you want to use it as per your requirement.

Hope this will help you.