-1

我想添加最後一個標記,如果位置有多個確切的經度和緯度。如何僅在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); 

}]) 

但是代碼將添加多個標記上的同一位置,所以在這種情況下,我有五個完全相同的位置和標記重疊五次對方,這不好。您可以參考下面的圖片:

enter image description here

我要的是隻顯示一個標記(這是最後的標誌物),上面有標籤的「5」。

任何想法如何做到這一點?

謝謝。

+0

我認爲你的代碼正在做你正在告訴它做的事情。當有多個標記時,您仍然將其添加到標記集羣。此外,因爲當您在配置中傳遞'地圖'時創建標記,該標記將被附加到該地圖上。您需要在創建新標記之前進行檢查,或者在檢查之前避免將標記分配給地圖。 –

回答

0

未經測試,但試試這個。

.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; 
    }); 
}; 

    $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)); 
    }); 
}; 


function createMarker(latlng, info, data) { 
    //Get array of markers currently in cluster 
    var allMarkers = mc.getMarkers(); 

    // Filter to duplicate markers 
    var dupeMarkers = allMarkers.filter(function (existingMarker) { 
     var pos = existingMarker.getPostion(); 
     return latlng.equals(pos) 
    }); 

    // Remove duplicate markers 
    dupeMarkers.forEach(function(dupeMarker) { 
     mc.removeMarker(dupeMarker); // remove from cluster 
     dupeMarker.setMap(null); // remove from map 
    }); 

    // Add marker to the map 
    var newMarker = new google.maps.Marker({ 
     position: latlng, 
     map: map 
    });    

    // Add marker to the cluster 
    mc.addMarker(newMarker); 

    // If you want to set the label to count of markers that are/were at this location 
    marker.setLabel(dupeMarkers.length + 1); 

    google.maps.event.addListener(newMarker, 'click', function() { 
     infowindow.close(); 
     infowindow.setContent(info); 
     infowindow.open(map, newMarker); 
    }); 

    return newMarker; 
} 


$scope.loadData() 
    .then($scope.initMap);  
}]); 
+0

這個缺陷是因爲代碼是去除了不知道標記之前存在的僞標記。您可以修改代碼以跟蹤計數並將其增加。 –

+0

嗨@Ray,你能告訴我我應該把櫃檯放在哪裏嗎? – CoolTips2u

+0

嗨@Ray,我試過你的代碼,它不工作。它仍然像我之前的代碼一樣添加多個標籤。 – CoolTips2u

相關問題