我希望对地址进行地理编码并将其转换为 GPS 坐标。基本上,
var address;
// Google/Yahoo/whatever program to convert address to GPS coordinates
var output=xx.xxxxxxx,xx.xxxxxxx
我研究过 Google 和 Yahoo API,但似乎无法让他们的代码在我的 Google 小工具中运行。有什么建议吗?
提前致谢!
最佳答案
这是我为满足地址到 GPS 坐标的需求所做的工作:
function get_coords(address)
{
var gc = new google.maps.Geocoder(),
opts = { 'address' : address };
gc.geocode(opts, function (results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
var loc = results[0].geometry.location,
lat = loc.$a,
long = loc.ab;
// Success. Do stuff here.
}
else
{
// Ruh roh. Output error stuff here
}
});
}
然后您可以像 get_coords('1600 Pennsylvania Avenue NW Washington, DC 20500')
那样调用它,它会返回纬度和经度。
当然,您需要提供自己的 Google Maps API key 才能使其正常工作。
希望这对您有所帮助!
关于javascript - 将地址地理编码为 GPS 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11318347/