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

时间:2015-05-06 21:54:12

标签: javascript google-maps google-maps-api-3 geocoding

我一直在尝试从URL变量提供的地址生成lat / long并在Google地图上显示,但地图是空白的(lat / long显然未定义)。问题似乎在于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);

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

***发现问题:

当我应该使用coords_obj.A和coords_obj.F作为我的console.log的输出时,我正在使用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]

1 个答案:

答案 0 :(得分:2)

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

<p:selectManyCheckbox ... collectionType="java.util.LinkedHashSet">