Distance between a point and several locations

陌路散爱 提交于 2019-12-11 18:34:20

问题


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.



来源:https://stackoverflow.com/questions/9877455/distance-between-a-point-and-several-locations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!