我正在与我一起列出旅游地点的清单,并且我愿意编写一项服务,该服务可以告诉我哪个旅游地点离我最近,然后第二近,并且同样不用地图。我该怎么做。我的想法是,我拥有一个城市中所有旅游地点及其经度/纬度的数据库,我可能在城市中的任何地方,并且想要找到哪个旅游胜地距离我最近,第二近且如此明智,因此我可能打算根据我有多少时间去拜访他们。我尝试了这个,发现了谷歌地图api,但我不想显示相同的地图。或为用户提供地图以搜索内容。
如果您不打算显示地图,那么您将无法使用Google Maps API(它违反了其TOS)。
[如果您希望从没有Google Maps或类似地址的地址获取经纬度(因为类似服务的TOS相似),那么您将需要查找LiveAddress API之类的东西(显然我应该透露我在SmartyStreets工作]
此示例适用于美国地址。国际地址需要使用其他API。
[类似US Street Address API的API不需要您显示地图,返回地理坐标,并会在返回有效载荷时验证地址的有效性。
<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>
您不需要Google地图。