我随身携带一份旅游地点列表,我愿意编写一项服务,告诉我哪个旅游地点离我最近,然后是第二近,同样也不需要使用地图。我该怎么做呢?我的想法是,我有一个城市中所有旅游景点及其纬度/经度的数据库,我可能在城市的任何地方,想找到哪个旅游景点离我最近,第二个离我最近。这样我就可以根据我有多少时间来计划参观它们。我尝试了一下,发现了谷歌地图api,但我不想显示同样的地图。或者给用户地图来搜索东西。
如果你不打算显示地图,那么你就不能使用谷歌地图API(这违反了他们的TOS)。
如果你想从一个没有谷歌地图或类似的地址获取lat/lon(因为类似的服务有类似的TOS),那么你会想寻找LiveAddress API之类的东西(显然我应该透露我在SmartyStreets工作)
此示例适用于美国地址。国际地址需要不同的API。
像美国街道地址API这样的API不需要显示地图,返回地理坐标,并在返回有效载荷时验证地址的有效性。
下面是一个Javascript示例。
<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>
您不需要谷歌地图。
- 通过地理定位API获取用户的位置
- 绘制点列表,使用大圆距离算法计算用户和点之间的距离来增加对象
- 通过距离对列表进行排序