我想问一下我的代码是错误还是错误的代码排列。 侧边栏显示,信息窗口也显示。但标记仍然无法聚集。(为什么?)
<!DOCTYPE html >
<html>
<head>
<title>Google Maps</title>
<script type="text/javascript" src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
</head>
<body >
<table border=1>
<tr>
<td>
<div id="map" style="width: 550px; height: 450px"></div>
</td>
<td width = 150 valign="top" style="text-decoration: underline; color: #4444ff;">
<div id="side_bar"></div>
</td>
</tr>
</table>
<script type="text/javascript">
// this variable will collect the html which will eventually be placed in the side_bar
var side_bar_html = "";
// arrays to hold copies of the markers and html used by the side_bar
// because the function closure trick doesnt work there
var gmarkers = [];
// A function to create the marker and set up the event window
function createMarker(point,name,html) {
var marker = new GMarker(point);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(html);
});
// save the info we need to use later for the side_bar
gmarkers.push(marker);
// add a line to the side_bar html
side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + name + '<\/a><br>';
return marker;
}
// This function picks up the click and opens the corresponding info window
function myclick(i) {
GEvent.trigger(gmarkers[i], "click");
}
// create the map
var map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng( 43.907787,-79.359741), 8);
// add the points
var point = new GLatLng(43.65654,-79.90138);
var marker = createMarker(point,"This place","Some stuff to display in the<br>First Info Window")
map.addOverlay(marker);
var point = new GLatLng(43.91892,-78.89231);
var marker = createMarker(point,"That place","Some stuff to display in the<br>Second Info Window")
map.addOverlay(marker);
var point = new GLatLng(43.82589,-78.89231);
var marker = createMarker(point,"The other place","Some stuff to display in the<br>Third Info Window")
map.addOverlay(marker);
var markers = [];
var markerCluster = new MarkerClusterer(map, markers, {
imagePath: 'https://raw.githubusercontent.com/googlemaps/v3-utility-library/master/markerclustererplus/images/m2.png',
gridSize: 10,
minimumClusterSize: 2
});
var mc = new MarkerClusterer(map, markers);
// put the assembled side_bar_html contents into the side_bar div
document.getElementById("side_bar").innerHTML = side_bar_html;
</script>
</body>
</html>
希望有人可以看看这个,感谢您的帮助。谢谢。
答案 0 :(得分:1)
你遇到了一些问题。
第一个问题:
你不应该使用Content here ... Test 1 2 3 4
,因为它自2013年以来一直被弃用。谷歌已经重写了它的地图javascript库,使其更轻巧,兼容移动设备,它是版本3.
V3示例:
gMap2
第二个问题:
数组 var map = new google.maps.Map(document.getElementById("map"),
{
zoom: 6,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
为空。您正在正确地创建markers
对象,但它已被提供一个空数组。它应该是您希望clusterer对象考虑进行聚类的标记数组。
MarkerClusterer
第3个问题:
您反复重新实例化变量 var markers = [];
...
var mc = new MarkerClusterer(map, markers);
。尝试为它们指定不同的名称,例如marker
,marker1
...这样您以后可以在代码中引用这些标记。
marker2
第4个问题
这是 MOST 重要的一个。您不应该 EXPOSE 您的Google地图API密钥对公众!这让我有机会对其进行恶意攻击,我相信您可能违反了许可协议。请不要再这样做了。我会修改你的帖子以审查那部分。
这是我之前在JSFiddle中创建的一个聚类器示例。