1
我有點困惑與谷歌地圖工作。 Iam不是編程的初學者,但JavaScript並不是我的最佳實踐。谷歌地圖 - 可點擊標記的標記羣集
我使用Google Maps Marker Cluster來標記某個位置。我從我的數據庫中獲取座標。但我想顯示(可點擊)說明(工具提示)。我沒有得到如何在我的代碼中實現這一點。
我的代碼如下所示:
function initMap(position) {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: {lat: 51.1427552, lng: 8.2123375}
});
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
}
// Create an array of alphabetical characters used to label the markers.
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// Add some markers to the map.
// Note: The code uses the JavaScript Array.prototype.map() method to
// create an array of markers based on a given "locations" array.
// The map() method here has nothing to do with the Google Maps API.
var markers = locations.map(function (location, i) {
console.log(location[2]);
return new google.maps.Marker({
position: location,
label: labels[i % labels.length],
});
});
console.log(markers);
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
var locations = [{"lat":52.2885818,"lng":7.4184098,"title":"test"},{"lat":52.2756548,"lng":7.4223696,"title":"test"}]
我希望你能告訴我我該怎麼整合這些工具提示。
我發現這樣的代碼:
google.maps.event.addListener(markers,'click',function() {
var infowindow = new google.maps.InfoWindow({
content:"Hello World!"
});
infowindow.open(地圖,標記); });
但我不知道如何實現。幫助將被磨擦。非常感謝!
你嘗試閱讀本:https://developers.google.com/maps/documentation/javascript/examples/event-simple –
是的,我的問題是整合在我的循環 – yfain