gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:53:50 25 4
gpt4 key购买 nike

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

最佳答案

谢谢geocodezip。为了避免 future 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);

})
}

关于javascript - 使用 Google Maps API 对 LatLong 进行地理编码返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30088301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com