我正在使用MarkerWithLabel.js在谷歌地图上显示多个标记,并在每个标记上附加标签。 标签正确显示,但我只显示一个标记。 究竟我错过了什么。我一直在寻找解决方案,但没有工作。 以下是我正在使用的javascript代码。 请帮忙!!!
var markers = <?php print(json_encode(str_replace('/','',$markers)))?>;
window.onload = function() {
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var infoWindow = new google.maps.InfoWindow();
for (i = 1; i <= markers.length; i++) {
var data = markers[i - 1]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new MarkerWithLabel({
position: myLatlng,
map: map,
title: data.title,
labelContent: i,
labelAnchor: new google.maps.Point(7, 30),
labelClass: "labels", // the CSS class for the label
labelInBackground: false
});
(function(marker, data) {
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
当json_encoded看起来类似于此时的Var标记。
var markers = [{
"title": 'Gombak Setia',
"lat": '3.218937',
"lng": '101.7167931',
"description": 'Gombak Setia, Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur, 53100'
}, {
"title": 'Jalan Desa Melawati',
"lat": '3.2331742',
"lng": '101.7061447',
"description": 'Jalan Desa Melawati, Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur, 53100'
}, {
"title": 'Jalan Intan (Gombak)',
"lat": '3.22317',
"lng": '101.71588',
"description": 'Jalan Intan (Gombak), Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur, 53100'
}, {
"title": 'Jalan Madrasah',
"lat": '3.218515',
"lng": '101.717801',
"description": 'Jalan Madrasah, Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur, 53100'
}];