How do i find distance between one place to anothe

2019-06-08 13:42发布

I am having a list of tourist locations with me and i am willing to write a service that could tell me which tourist location is nearest to me and then second nearest and likewise without using a map. How can i do it. The idea is that i have a database of all the tourist locations in a city and their lat/long, I may be in any place in the city and want to find which tourist attraction is nearest to me and second nearest and like wise so that i may plan to visit them based on how much time i have. I tried this and found google maps api but i dont want to display a map for the same. Or give users map to search things.

2条回答
Bombasti
2楼-- · 2019-06-08 14:03

If you're not going to display a map, then you can't use Google Maps API (it violates their TOS).

If you are looking to get the lat/lon from an address without Google Maps or similar (because similar services have similar TOS) then you'll want to look for something like LiveAddress API (and apparently I'm supposed to disclose that I work at SmartyStreets) -- this example works for US addresses. International addresses require a different API.

An API like LiveAddress doesn't require you to show a map, returns geo coordinates, and will verify the validity of the address as it returns its payload.

Here's a Javascript example.

<script type="text/javascript" src="liveaddress.min.js"></script>
<script type="text/javascript">
LiveAddress.init(123456789); // API key

// Make sure you declare or obtain the starting or ending lat/lon somewhere.
// This example only does one of the points.

LiveAddress.geocode(addr, function(geo) {
    var lat2 = geo.lat, lon2 = geo.lon;

    // Distance calculation from: http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points
    var R = 6371; // Radius of the earth in km
    var dLat = (lat2-lat1).toRad();  // Javascript functions in radians
    var dLon = (lon2-lon1).toRad(); 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
            Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; // Distance in km
});
</script>
查看更多
走好不送
3楼-- · 2019-06-08 14:09

You don't need Google maps.

  1. Get the user's location via the geolocation API.
  2. Map over the list of points, augmenting your object with the distance between the user and the point as calculated using the great-circle distance algorithm.
  3. Sort the list via the distance.
查看更多
登录 后发表回答