-->

Geoencode地址到GPS坐标(Geoencode Address to GPS Coordin

2019-09-19 01:25发布

我期待geoencode的地址,并将其转换为GPS坐标。 基本上,

var address;

//  Google/Yahoo/whatever program to convert address to GPS coordinates

var output=xx.xxxxxxx,xx.xxxxxxx

我研究谷歌和雅虎的API,但似乎无法得到他们的代码在我的谷歌小工具的工作。 有什么建议么?

提前致谢!

Answer 1:

下面是我为我的地址到GPS-COORDS需求:

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')它会返回经度和纬度。

你需要提供你自己的谷歌地图API密钥,使其工作,当然。

希望这可以帮助!



Answer 2:

我帮助维持一个所谓的API LiveAddress它做到这一点。 更容易使用比谷歌/雅虎的API,并且没有限制的服务条款,禁止申请集体 ,存储结果,或使用数据而不显示图表或通过自动化。

这里有一个完整的例子,使用此示例包装函数 :

LiveAddress.init(123456789); // your API key here
LiveAddress.geocode("address goes here", function(geo) {
    // You can also pass in an array of addresses,
    // the ID of a DOM element containing the address,
    // or an array of IDs
    console.log(geo);
});

各个坐标被发现在geo.latgeo.lon ,用组合字符串“纬度,经度”格式geo.coords 。 您还可以获取与数据的精度geo.precision



Answer 3:

我这样做,并努力完善:

<script type="text/javascript">
var dir1 = "5th ave, new york";
var google_url = "http://maps.googleapis.com/maps/api/geocode/json?address=";
var sensor = "&sensor=false";
var resultado;

function myFunction(){ 

    $.getJSON(
        google_url+dir1+sensor,
        function(result){
            $( "body" ).append( "Latitude: " + JSON.stringify(result.results[0].geometry.bounds.northeast.lat) )
            $( "body" ).append( "Longitude: " + JSON.stringify(result.results[0].geometry.bounds.northeast.lng) )  
    });

}



文章来源: Geoencode Address to GPS Coordinates