从Geoencode地址到GPS坐标

问题描述 投票:1回答:3

我想对一个地址进行地理编码,并将其转换为GPS坐标。 基本上..,

var address;

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

var output=xx.xxxxxxx,xx.xxxxxxx

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

先谢谢你

javascript google-maps geolocation yahoo-maps
3个回答
4
投票

这是我为我的地址到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') 它将返回经纬度。

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

希望这能帮到你


2
投票

我帮助维护一个叫做 美国街道地址 它可以做到这一点。比GoogleYahoo的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.


1
投票

我这样做,工作完美。

<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) )  
    });

}

© www.soinside.com 2019 - 2024. All rights reserved.