我已經有了完全正常的地圖,但不確定為什么我不能讓任何標記或集群出現在這上面?我的JS代碼如下:
function initMap() {
var map = new google.maps. Map(document.getElementById("map"/span>), {
zoom: 12,
center: { lat: 51.40219, lng: -0.16890 }.
});
var labels = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
}
//Coordinates for the various Memorials and Cemetries listed for the corresponding Hero that will be marked on the map..
//我用https://codepen.io/ahmadawais/pen/NQdWQx?editors=1010,看看如何用位置來做下面的作業,因為這樣會和紀念館或墓地的名字一起顯示。
var locations = [...
['Private Richard Spearink - St Souplet British Cemetery', { lat: 50.05484, lng: 3.52440 }, 1] 。
['Private Frederick Spearink - Mitcham War Memorial', { lat: 51.40219, lng: -0.16890 }, 2】。]
['Private Frederick Spearink - Helles Memorial', { lat: 40.04597, lng: 26.17921}, 2].
];
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
label: labels[i % labels.length] 。
});
});
var markerCluster = new MarkerClusterer(map, markers, { imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m' })。)
誰能告訴我,我需要添加或洗掉什么,因為我不確定為什么這不作業?
uj5u.com熱心網友回復:
你沒有為你的標記正確傳遞你的經緯度。
你的locations陣列是一個固定長度的陣列("tuples"),其中第二個是一個lat和lng的物件,與LatLngLiteral匹配。這就是MarkerOptions中position的正確值,但是在你的locations.map中,你傳遞了location,這里是整個元組。
因為每個條目都不是一個location,我把location重命名為locationTuple;這些名字對代碼來說并不重要,但它確實讓人更清楚地知道你需要使用locationTuple[1](或location)來參考你對position的值。在這一點上,你也可以將標簽作為locationTuple[0]來使用,而不是任意挑選一個字母。
var locations = [
['Private Richard Spearink - St Souplet British Cemetery', { lat: 50.05484, lng: 3.52440 }, 1] 。
['Private Frederick Spearink - Mitcham War Memorial', { lat: 51.40219, lng: -0.16890 }, 2】。]
['Private Frederick Spearink - Helles Memorial', { lat: 40.04597, lng: 26.17921}, 2].
];
var markers = locations.map(function(locationTuple, i) { /span> rename
return new google.maps.Marker({
position: locationTuple[1], //重命名 添加索引1。
label: locationTuple[0] //重命名 添加索引0。
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/311661.html
標籤:
