【问题标题】:Geoencode Address to GPS Coordinates将地址地理编码为 GPS 坐标
【发布时间】:2012-07-04 07:46:18
【问题描述】:

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

var address;

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

var output=xx.xxxxxxx,xx.xxxxxxx

我研究了 Google 和 Yahoo API,但似乎无法让它们的代码在我的 Google 小工具中运行。有什么建议吗?

提前致谢!

【问题讨论】:

  • Google API 和 Yahoo API 都执行此操作,因此我的建议是发布您的无效代码。

标签: javascript google-maps geolocation yahoo-maps


【解决方案1】:

我帮助维护一个名为 LiveAddress 的 API,它可以做到这一点。比 Google/Yahoo 的 API 更易于使用,并且没有限制性的服务条款,这些条款禁止大量请求、存储结果或在不显示地图或通过自动化方式使用数据。

这是一个完整的例子,使用this sample wrapper function

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 中使用组合字符串“lat, lon”格式。也可以通过geo.precision获取数据的精度。

【讨论】:

    【解决方案2】:

    我做到了,而且效果很好:

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

    【讨论】:

      【解决方案3】:

      这是我为地址到 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') 这样调用它,它会返回纬度和经度。

      当然,您需要提供自己的 Google Maps API 密钥才能使其正常工作。

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2023-04-04
        • 2012-05-30
        • 2012-05-21
        • 1970-01-01
        • 1970-01-01
        • 2020-01-16
        • 1970-01-01
        • 2013-05-06
        • 2016-09-09
        相关资源
        最近更新 更多