繁体   English   中英

使用Google Maps API对LatLong进行地理编码返回未定义

[英]Geocoding LatLong with Google Maps API Returns Undefined

我一直在尝试从URL变量提供的地址生成经度/纬度并显示在Google地图上,但该地图是空白的(经度/纬度显然未定义)。 问题似乎出在getCoordinates函数上,但我不知道它是什么。

我的代码:

<?php $address = $_GET["address"]; ?>

<script>
geocoder = new google.maps.Geocoder();

window.getCoordinates = function (address, callback) {
    var coordinates;
    geocoder.geocode({ address: address}, function (results, status) {
        coords_obj = results[0].geometry.location;
        coordinates = [coords_obj.nb,coords_obj.ob];
        callback(coordinates);
    })
}
</script>

<script>

google.maps.visualRefresh = true;
var map;

function initialize() {


    getCoordinates('<?php echo $address ?>', function(coords) {
      var mapOptions = {
      center: new google.maps.LatLng(coords[0], coords[1]),
      zoom: 15,
      mapTypeId: google.maps.MapTypeId.ROADMAP
      };

      map = new google.maps.Map(document.getElementById('map-canvas'),
          mapOptions);

})

}

google.maps.event.addDomListener(window, 'load', initialize);

任何帮助,将不胜感激。 谢谢。


***发现问题:

我本应该使用console.log输出的coords_obj.A和coords_obj.F时使用的是coords_obj.nb和coords_obj.ob:

[Object]
0: Object
address_components: Array[8]
formatted_address: "1045 Mission Street, San Francisco, CA 94103, USA"
geometry: Object
location: pf
A: 37.7800206
F: -122.40959459999999
__proto__: pf
location_type: "ROOFTOP"
viewport: ph
__proto__: Object
place_id: "ChIJeaMNfoSAhYARY4zPZwV_vgw"
types: Array[1]
__proto__: Object
length: 1
__proto__: Array[0]

谢谢geocodezip。 为了避免将来的API更新中断,我将其替换为:

geocoder = new google.maps.Geocoder();

window.getCoordinates = function (address, callback) {
    var coordinates;
    geocoder.geocode({ address: address}, function (results, status) {
        coords_obj_lat = results[0].geometry.location.lat();
        coords_obj_lng = results[0].geometry.location.lng();
        coordinates = [coords_obj_lat,coords_obj_lng];
        callback(coordinates);

    })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM