我正在开发一个使用具有 127 个标记的地图标记集群的项目。我需要帮助的是当访问者单击标记列表中的文本链接时打开一个 infoWindow。目前,单击文本会缩放到标记,但不会同时打开 infoWindow。单击标记确实会打开信息窗口。我在代码中添加了一条注释来指出生成标记列表的位置。注释是 // 可点击标记的文本列表。
不幸的是,该网站正在开发中,无法公开显示。让我知道是否有任何其他信息可以帮助您帮助我。提前致谢。
var globalMarker = [];
var map;
function initialize() {
var center = new google.maps.LatLng(35.4214, -15.6919);
map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 1,
center: center,
disableDoubleClickZoom: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var markers = [];
var infowindow = new google.maps.InfoWindow();
for (i = 0; i < 127; i++) {
var dataChapters = data.chapters[i];
var latLng = new google.maps.LatLng(dataChapters.latitude, dataChapters.longitude);
marker = new MarkerWithLabel({
position: latLng,
draggable: true,
raiseOnDrag: true,
map: map,
labelContent: "<strong>" + dataChapters.name + "<\/strong>" + "<br />" + dataChapters.description + "<br /><br />", // label for cluster view.
labelAnchor: new google.maps.Point(30, 0),
labelVisible: false,
labelClass: "labels", // the CSS class for the label
labelStyle: {
opacity: 0.75
}
});
markers.push(marker);
// text list of clickable markers
makeDiv(i, 15, dataChapters.rank + ") " + "<span class=\"chapterlink\">" + dataChapters.name + "<\/span>" + "<br />");
google.maps.event.addListener(markers[i], 'click', function (e) {
infowindow.setContent(this.labelContent); // label for pin.
infowindow.open(map, this);
});
}
var clusterOptions = {
zoomOnClick: true
};
var markerCluster = new MarkerClusterer(map, markers, clusterOptions);
globalMarker = markers.slice();
google.maps.event.addListener(markerCluster, 'clusterclick', function (cluster) {
var content = '';
// Convert lat/long from cluster object to a usable MVCObject
var info = new google.maps.MVCObject;
info.set('position', cluster.center_);
//----
//Get markers
var markers = cluster.getMarkers();
var titles = "";
//Get all the titles
for (var i = 0; i < markers.length; i++) {
titles += markers[i].labelContent + "\n";
}
//---
infowindow.close();
infowindow.setContent(titles); //set infowindow content to titles
infowindow.open(map, info);
google.maps.event.addListener(map, 'zoom_changed', function () {
infowindow.close();
});
});
}
function makeDiv(index, zoomLevel, content) {
document.getElementById("sidebar").innerHTML += '<div class="child" onclick="zoomIn(' + index + ',' + zoomLevel + ')">' + content + ' </div>';
}
function zoomIn(index, zoomLevel) {
map.setCenter(globalMarker[index].getPosition());
map.setZoom(zoomLevel);
}
google.maps.event.addDomListener(window, 'load', initialize);