My main task is to update the markers on the map.
Notation:
Markers that are displayed in real-time on the map:
var markers = [
{ 'lat':10, 'lng':10, 'type':'simple'},
{ 'lat':20, 'lng':20, 'type':'simple'},
{ 'lat':40, 'lng':40, 'type':'cluster'}
]
New markers which should be on the map:
var newMarkers = [
{ 'lat':10, 'lng':10, 'type':'simple'},
{ 'lat':20, 'lng':20, 'type':'simple'},
{ 'lat':30, 'lng':30, 'type':'simple'},
{ 'lat':50, 'lng':50, 'type':'simple'}
]
Thus the problem is reduced to the subtask which I want to find solution:
update the array of objects - markers from the another array of objects - newMarkers.
Thus, need to perform the following manipulation with markers array:
- Remove objects from
markerswhich are not innewMarkers(compared by three properties:lat,lng,type). - Add objects from
newMarkersintomarkersif not exist (compared bylat,lng). If marker exist (by two properies:lat,lng) is necessary to update it, i.e. to replace by a new marker fromnewMarkers.
My solution ineffective since it is because it is performed for a long time on large number of markers.
Updated markers array should look like:
console.log(markers)
{ 'lat':10, 'lng':10, 'type':'simple'},
{ 'lat':20, 'lng':20, 'type':'simple'},
{ 'lat':30, 'lng':30, 'type':'simple'},
{ 'lat':50, 'lng':50, 'type':'simple'}
newMarkers, and I don't see how it could ever be different - you're effectively getting rid of anything not in the newMarkers arraymarkers = newMarkers, judging on your example dataset.