【问题标题】:Google Maps: Getting coordinates谷歌地图:获取坐标
【发布时间】:2015-09-20 15:42:37
【问题描述】:

使用 AngularJSGoogle Maps API 我使用以下代码从地址获取坐标:

  var geocoder = new google.maps.Geocoder();
            geocoder.geocode({'address': address}, function (results, status) {

                if (status === google.maps.GeocoderStatus.OK) {
                    var lat = results[0].geometry.location.A || results[0].geometry.location.G || results[0].geometry.location.H;
                    var lng = results[0].geometry.location.F || results[0].geometry.location.K || results[0].geometry.location.L;

                   //Something to do
            });

当我开始这个项目时,我只写了这个:

var lat = results[0].geometry.location.A 
var lng = results[0].geometry.location.F;

随着时间的推移,Google Maps API 将变量的名称更改为 location.G 和 location.K,我需要为此重新编辑代码:

var lat = results[0].geometry.location.A || results[0].geometry.location.G;
var lng = results[0].geometry.location.F || results[0].geometry.location.K;

现在,Google Maps API 再次将变量名称更改为 location.H 和 location.L...

新代码:

var lat = results[0].geometry.location.A || results[0].geometry.location.G || results[0].geometry.location.H;
var lng = results[0].geometry.location.F || results[0].geometry.location.K || results[0].geometry.location.L;

我不想编辑更多我的代码,我希望我的代码稳定...有什么方法可以让坐标更稳定?

谢谢!

【问题讨论】:

    标签: angularjs google-maps google-maps-api-3


    【解决方案1】:

    这看起来像是这个问题的重复:

    google.maps.Geocoder.geocode() geometry.location lat/lng property names change frequently

    答案是:

    "使用记录的属性,它们不会改变。geometry.location 是一个google.maps.LatLng 对象,记录的方法是: lat() number 以度数返回纬度。 lng() number 返回以度为单位的经度。"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      相关资源
      最近更新 更多