将地址地理编码为 GPS 坐标

Geoencode Address to GPS Coordinates

本文关键字:GPS 坐标 编码 地址      更新时间:2023-09-26

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

var address;
//  Google/Yahoo/whatever program to convert address to GPS coordinates
var output=xx.xxxxxxx,xx.xxxxxxx

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

提前感谢!

以下是我为我的地址到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密钥才能使其正常工作。

希望这有帮助!

我这样做并且工作得很好:

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

我帮助维护一个名为LiveAddress的API。比谷歌/雅虎的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,组合字符串"lat, lon"格式为geo.coords。您还可以通过geo.precision获得数据的精度。