I have created an Angular JS module that utilizes the Google Map API. The map is defined as a global variable, along with a global array for markers, and functions for managing these markers such as removing them, resetting the map, and adding new markers.
angular.module('gmapService', []).factory('gmapService', function($rootScope, $http){
var googleMapService = {};
var map;
var markers = [];
function removeExistingMarkersFromTheMap() {
for (var i = 0; i < markers.length; i++ ) {
markers[i].setMap(null);
}
markers.length = 0;
}
var resetMap = function() {
var oldZoom = map.zoom;
var oldCenter = map.center;
map = new google.maps.Map(document.getElementById('map'), {
zoom: oldZoom,
center: oldCenter
});
removeExistingMarkersFromTheMap();
}
googleMapService.placeMarkersOnMap = function(map, markersToPut) {
// querying the data needed to place markers on the map
for(var i=0; i<data.length; i++) {
var marker = new google.maps.Marker({
position: latLng,
icon: icon
});
markers.push(marker);
}
var mcOptions = {gridSize: 60, minZoom: 4, maxZoom: 12};
var mc = new MarkerClusterer(map, markers, mcOptions);
}
return googleMapService;
}
Currently, my process involves calling resetMap(), followed by placeMarkersOnMap(). This approach works fine, but I am seeking a way to update the displayed data on the map without having to reload the entire map itself.
Unfortunately, when I only execute removeExistingMarkersFromTheMap() and then placeMarkersOnMap(), the old data still persists. What could be causing this issue?