gpt4 book ai didi

google-maps - 未捕获的类型错误 : Cannot read property 'getZoom' of undefined

转载 作者:行者123 更新时间:2023-12-05 00:59:40 27 4
gpt4 key购买 nike

我是谷歌地图的新手,如果这是一个愚蠢的问题,请原谅。

我正在尝试将标记集群选项与 Google map 一起使用

这是我的代码

var map;
var global_markers = [];
var markers = [
[37.09024, -95.712891, 'trialhead0'],
[37.09024, -95.712891, 'trialhead1'],
[37.09024, -95.712892, 'trialhead2']
];
var infowindow = new google.maps.InfoWindow({});

function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(40.77627, -73.910965);
var myOptions = {
zoom: 1,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
addMarker();
}

function addMarker() {
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i][0]);
var lng = parseFloat(markers[i][1]);
var trailhead_name = markers[i][2];
var myLatlng = new google.maps.LatLng(lat, lng);
var contentString = "<html><body><div><p><h2>" + trailhead_name + "</h2></p></div></body></html>";
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: "Coordinates: " + lat + " , " + lng + " | Trailhead name: " + trailhead_name
});
marker['infowindow'] = contentString;
global_markers[i] = marker;
global_markers.push(marker);
google.maps.event.addListener(global_markers[i], 'click', function() {
infowindow.setContent(this['infowindow']);
infowindow.open(map, this);
});
}
}
var markerCluster = new MarkerClusterer(map, global_markers);
window.onload = initialize;

当我运行此代码时,我在浏览器控制台下收到以下异常

未捕获的类型错误:无法读取未定义的属性“getZoom”

这是我的 fiddle

http://jsfiddle.net/ZLuTg/1023/

这个怎么解决??

最佳答案

map仅在窗口 onload 处理程序中实例化,但它被传递给 MarkerCluster构造函数作为它之前的参数。这就是它未定义的原因。

确保 MarkerClustermap 之后构建.

http://jsfiddle.net/ZLuTg/1025/

关于google-maps - 未捕获的类型错误 : Cannot read property 'getZoom' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30398149/

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