我之前从地址字符串中获取了页面加载时的所有标记坐标,我现在知道它非常糟糕。我重构了代码,它工作得很好但是clustermarkerer无法加载。
所有我进入萤火虫的是:
TypeError: marker.getPosition is not a function
return bounds.contains(marker.getPosition());
我一直在尝试很多事情,所以可能会有一两个不必要的线。
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
@*<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>*@
<script type="text/javascript">
var script = '<script type="text/javascript" src="../Scripts/markerclusterer';
if (document.location.search.indexOf('compiled') !== -1) {
script += '_compiled';
}
script += '.js"><' + '/script>';
document.write(script);
</script>
<script>
var bounds = new google.maps.LatLngBounds();
var latlng = new google.maps.LatLng(21.0000, 78.0000);
var map = new google.maps.Map(document.getElementById("map"), {
zoom: 5,
styles: [{ featureType: "landscape", stylers: [{ saturation: -100 }, { lightness: 65 }, { visibility: "on" }] }, { featureType: "poi", stylers: [{ saturation: -100 }, { lightness: 51 }, { visibility: "simplified" }] }, { featureType: "road.highway", stylers: [{ saturation: -100 }, { visibility: "simplified" }] }, { featureType: "road.arterial", stylers: [{ saturation: -100 }, { lightness: 30 }, { visibility: "on" }] }, { featureType: "road.local", stylers: [{ saturation: -100 }, { lightness: 40 }, { visibility: "on" }] }, { featureType: "transit", stylers: [{ saturation: -100 }, { visibility: "simplified" }] }, { featureType: "administrative.province", stylers: [{ visibility: "off" }]/**/ }, { featureType: "administrative.locality", stylers: [{ visibility: "off" }] }, { featureType: "administrative.neighborhood", stylers: [{ visibility: "on" }]/**/ }, { featureType: "water", elementType: "labels", stylers: [{ visibility: "on" }, { lightness: -25 }, { saturation: -100 }] }, { featureType: "water", elementType: "geometry", stylers: [{ hue: "#ffff00" }, { lightness: -25 }, { saturation: -97 }] }],
center: new google.maps.LatLng(63.12816100000001, 17.643501000000015),
mapTypeId: google.maps.MapTypeId.ROADMAP,
panControl: true,
panControlOptions: {
position: google.maps.ControlPosition.RIGHT_TOP
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.RIGHT_TOP
}
});
var markers = [];
jQuery(function ($) {
var mcOptions = {
styles: [{
height: 36,
url: "../images/music_rock.png",
width: 32,
textColor: "white",
textSize: 20,
}]
}
$.ajax({
type: "GET",
datatype: 'json',
url: "/Handler/GetLocations.ashx",
data: "",
success: function (data) {
$(data).each(function (i) {
var loaction = [data[i].Address, data[i].Latitude, data[i].Langitude, data[i].index];
markers.push(loaction);
});
console.log(markers);
console.log(map);
console.log(mcOptions);
initializer(markers);
var markerCluster = new MarkerClusterer(map, markers, mcOptions);
},
error: function (f, d, s) {
}
});
});
function initializer(locations) {
var infowindow = new google.maps.InfoWindow();
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
animation: google.maps.Animation.DROP,
icon: "../images/musicMarker.png"
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
var latlng = new google.maps.LatLng(21.0000, 78.0000);
}
//bounds.extend(marker.position);
}));
}
}
</script>
答案 0 :(得分:0)
简单地说,标记数组并未完全填充。我添加了tempMarkers来推送具有所需属性的标记。
var tempMarkers = [];
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
animation: google.maps.Animation.DROP,
icon: "../images/musicMarker.png"
});
tempMarkers.push(marker);
}
然后将数组添加到clusterer:
markerClusterer = new MarkerClusterer(map, tempMarkers, mcOptions);
希望它可以帮助有同样错误的人!