我想添加最後一個標記,如果位置有多個確切的經度和緯度。如何僅在Google地圖上顯示多個確切位置上的最後一個標記與angularjs
這是我的控制器:
.controller('MapsController', ['$scope', '$http', function ($scope, $http) {
var map;
//Marker clusterer
var mc;
var mcOptions = { gridSize: 20, maxZoom: 17, imagePath: "images/m" };
//Global infoWindow
var infowindow = new google.maps.InfoWindow();
//Geocoder
var geocoder = new google.maps.Geocoder();
$scope.loadData = function() {
var url = 'data/LatLng.json';
return $http.get(url).then(function (response) {
return response.data;
});
};
function createMarker(latlng, info, data) {
var marker = new google.maps.Marker({
position: latlng,
map: map
});
//Get array of markers currently in cluster
var allMarkers = mc.getMarkers();
//Check to see if any of the existing markers match the latlng of the new marker
if (allMarkers.length != 0) {
var count = 1;
var morethanone = false;
for (var i = 0; i < allMarkers.length; i++) {
var existingMarker = allMarkers[i];
var pos = existingMarker.getPosition();
if (latlng.equals(pos)) {
info = info + info;
count = count + 1;
morethanone = true;
}
}
}
google.maps.event.addListener(marker, 'click', function() {
infowindow.close();
infowindow.setContent(info);
infowindow.open(map, marker);
});
if (morethanone) {
for (var a = 1; a < count; a++) {
if (a === (count - 1)) {
marker.setLabel(String(count));
mc.addMarker(marker);
}
}
}
else {
marker.setLabel(String(count));
mc.addMarker(marker);
}
console.log(count);
return marker;
}
$scope.initMap = function (data) {
var mapOptions = {
zoom: 7,
center: new google.maps.LatLng(3.9443, 101.6954),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var gmarkers = [];
mc = new MarkerClusterer(map, [], mcOptions);
data.forEach(function (item) {
var latlng = new google.maps.LatLng(item.LAT, item.LON);
var info = item.Cat + "," + item.Type;
gmarkers.push(createMarker(latlng, info, data));
});
};
$scope.loadData()
.then($scope.initMap);
}])
但是代碼將添加多個標記上的同一位置,所以在這種情況下,我有五個完全相同的位置和標記重疊五次對方,這不好。您可以參考下面的圖片:
我要的是隻顯示一個標記(這是最後的標誌物),上面有標籤的「5」。
任何想法如何做到這一點?
謝謝。
我認爲你的代碼正在做你正在告訴它做的事情。當有多個標記時,您仍然將其添加到標記集羣。此外,因爲當您在配置中傳遞'地圖'時創建標記,該標記將被附加到該地圖上。您需要在創建新標記之前進行檢查,或者在檢查之前避免將標記分配給地圖。 –