Google Api获取最近的位置

时间:2011-03-08 12:10:09

标签: google-maps geolocation

我希望通过传递位置地址来获取最近位置的列表。 像:洛杉矶,应该以json或xml格式给我附近所有位置的列表 为此目的有没有Google api?

4 个答案:

答案 0 :(得分:11)

Google Places API列出了指定点附近的地方。例如,您可以搜索英国伦敦Convent Garden附近的书店。可以在good example中找到Google Maps Javascript API V3 Places Library documentation

您还可以使用Geonames.org data dump并滚动自己的地理位置查询或使用他们的REST API。使用他们的数据库,您可以搜索各种类型的地理实体(参见feature codes)。

大多数现代SQL Server部署都有一些地理空间支持,如果您希望下载数据转储,通常会使用某些Haversine Formula来查找附近的结果。

但是,如果您只想使用他们的公共REST API,您会发现他们已经拥有Find Nearby Toponym方法,该方法采用特征代码和各种其他搜索参数并返回XML或JSON。

如果你愿意,这应该让你search for heliports within 50 km of San Francisco

如果你想看到这一点,结合谷歌地图,我刚刚把这些东西放在一起(你需要你自己的Geonames帐户)。 In case of a zombie attack you should avoid the following cemeteries in San Francisco

答案 1 :(得分:2)

'Google Geocoding API'这可以帮助您通过传递地址来查找最近位置列表。 供参考,请访问此链接

http://code.google.com/apis/maps/documentation/geocoding/

您可以通过传递地址来使用此API。也可以通过传递纬度和经度等坐标来找到列表

答案 2 :(得分:0)

我认为你可能意味着LocalSearch API被弃用了 - 现在是Places API

答案 3 :(得分:0)

请参考以下代码以正确理解脚本中提到的API以及 我的链接获得了代码,并帮助我理解了https://www.geodatasource.com/developers/javascript

function distance(lat1, lon1, lat2, lon2, unit) {
    var radlat1 = Math.PI * lat1/180
    var radlat2 = Math.PI * lat2/180
    var theta = lon1-lon2
    var radtheta = Math.PI * theta/180
    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    if (dist > 1) {
        dist = 1;
    }
    dist = Math.acos(dist)
    dist = dist * 180/Math.PI
    dist = dist * 60 * 1.1515
    if (unit=="K") { dist = dist * 1.609344 }
    if (unit=="N") { dist = dist * 0.8684 }
    return dist
}

var data = [{
    "code": "0001",
    "lat": "1.28210155945393",
    "lng": "103.81722480263163",
    "location": "Stop 1"
}, {
    "code": "0003",
    "lat": "1.2777380589964",
    "lng": "103.83749709165197",
    "location": "Stop 2"
}, {
    "code": "0002",
    "lat": "1.27832046633393",
    "lng": "103.83762574759974",
    "location": "Stop 3"
}];

var LocNear= "";
var poslat = 1.28210155945393;
var poslng = 103.81722480263163;

for (var i = 0; i < data.length; i++) {
    // if this location is within 0.1KM of the user, add it to the list
    if (distance(poslat, poslng, data[i].lat, data[i].lng, "K") <= 0.1) {
        LocNear+= '<p>' + data[i].location + ' - ' + data[i].code + '</p>';
    }
}

$('#nearbystops').append(LocNear);