gpt4 book ai didi

javascript - 谷歌地图 : Cannot read property 'geometry' of undefined

转载 作者:行者123 更新时间:2023-11-28 06:10:42 25 4
gpt4 key购买 nike

尝试在 map 上显示来自 JSON 源的标记时遇到错误。我收到很多地址,然后尝试获取地址的经度和纬度,然后将其放置在 map 上,但是,我收到错误:无法在控制台中读取未定义的属性“几何”。

代码:

$.getJSON(apiUrl, function( data ) {
var delay = 0,
p,
contentString,
iconBase;

_.each(data, function (value, key) {

var link = value.LinkUrl,
address = value.Address,
content = "<div class='info-address'>" + address + "</div>
<div class='info-message'>" + value.OpeningHoursMessage + "</div>
<div class='info-link'><a href='"+ value.LinkUrl +"'>Læs mere</a></div>";
console.log("Adresse: " + address);
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+address+', Danmark', null, function (data) {


p = data.results[0].geometry.location,
contentString = content,
iconBase = '/static/dist/img/';

//console.log("Værdi" + p);

var latlng = new google.maps.LatLng(p.lat, p.lng);
var marker = new google.maps.Marker({
position: latlng,
map: map,
animation: google.maps.Animation.DROP,
icon: iconBase + 'maps-marker.png',
url: link
});

//If single lookup, then center to position
if(DOM.dataAddress.length) {
var pos = {lat: lat, lng: lng};
map.setCenter(pos);
}

bindInfoWindow(marker, map, infowindow, contentString);

DOM.markersArray.push(marker);


});
});
});

最佳答案

看起来响应应该是一个数组 according to the docs 。也许您请求的地址之一没有任何结果?您应该在向 map API 发出 JSON 请求后进行检查。

$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+address+', Danmark', null, function (data) {
if (data.results.length > 0) {
// set `p` and other code
}
});

关于javascript - 谷歌地图 : Cannot read property 'geometry' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36431491/

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