我如何在不嵌入Google地图的情况下使用Geolocation或Similiar API查找一个地方到另一个地方之间的距离? [关闭]

问题描述 投票:1回答:2

我正在与我一起列出旅游地点的清单,并且我愿意编写一项服务,该服务可以告诉我哪个旅游地点离我最近,然后第二近,并且同样不用地图。我该怎么做。我的想法是,我拥有一个城市中所有旅游地点及其经度/纬度的数据库,我可能在城市中的任何地方,并且想要找到哪个旅游胜地距离我最近,第二近且如此明智,因此我可能打算根据我有多少时间去拜访他们。我尝试了这个,发现了谷歌地图api,但我不想显示相同的地图。或为用户提供地图以搜索内容。

geolocation google-places-api
2个回答
0
投票

如果您不打算显示地图,那么您将无法使用Google Maps API(它违反了其TOS)。

[如果您希望从没有Google Maps或类似地址的地址获取经纬度(因为类似服务的TOS相似),那么您将需要查找LiveAddress API之类的东西(显然我应该透露我在SmartyStreets工作]

此示例适用于美国地址。国际地址需要使用其他API。

[类似US Street Address API的API不需要您显示地图,返回地理坐标,并会在返回有效载荷时验证地址的有效性。

这里是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>

0
投票

您不需要Google地图。

  1. 通过geolocation API获取用户的位置。
  2. 映射到点列表上,通过使用great-circle distance算法计算出的用户和点之间的距离来扩展对象。
  3. 通过距离对列表进行排序。
© www.soinside.com 2019 - 2024. All rights reserved.