javascript - 将地址地理编码为 GPS 坐标

标签 javascript google-maps geolocation yahoo-maps

我希望对地址进行地理编码并将其转换为 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/

相关文章:

javascript - Ajax 调用具有多个返回值的服务

javascript - ReactDOM.render() 中的一个根元素还是整个布局?

javascript - 有没有办法只在图像的不透明度为 1 时单击?

javascript - 带有 OpenWeatherMap 瓦片图层覆盖的 Google Maps Javascript API

android - 如何通过 SDK 在每个 session 中实现 Google Places 自动完成功能?

java - 如何从 Mapreduce 作业查询存储在 hdfs 中的嵌入式数据库?

php - 如何在 PHP 项目中创建 GeoIP 功能?

javascript - 全局作用域中的函数表达式与函数声明的区别

java - 如何在android中找到两个区域之间的距离

javascript - Phonegap 未在 Angularjs 指令内触发 GoogleMaps v3 domListener 函数