How do i find distance between one place to another using Geolocation or Similiar API without embedding a Google Map? [closed]

一笑奈何 提交于 2019-12-06 14:38:24
Matt

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>

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