gmap3 v5 does reuse itself if it is allready initialized. You can see that in the code by yourself, $.fn.gmap3
:
gmap3 = $this.data("gmap3");
..
if (!gmap3){
gmap3 = new Gmap3($this);
..
You dont have to reinitialize your map over and over - just to reset / add new markers. Lets say you have initialized your gmap3 as in your code above, and have two buttons - #addMarker
and #clearMarkers
:
<button id="clearMarkers">clear</button>
<button id="addMarker">add</button>
Then you can add / remove markers on the fly like this :
//add a marker, this could be an array of markers / latlngs
$("#addMarker").click(function() {
$('#mapWrapper').gmap3({
marker:{
latLng : new google.maps.LatLng(46.578498, 2.457275)
}
});
});
//clear all markers on the map
$("#clearMarkers").click(function() {
$('#mapWrapper').gmap3({
clear: {
name:["marker"],
}
});
});
So you see, gmap3 already reuse itself.
However, if you have memory issues - why use an "expensive" library as gmap3 anyway? Would it not be better with a native google map, and using a map-instance to update the map with? That would certainly reduce the memory usage a lot.
But I am not totally convinced that it actually is memory you have a problem with. When people are facing problems with google maps on smartphones, it is most likely due to google maps caching of map-tiles, that quickly kan fill up the internal cache.
You can avoid googles map-tile caching completely - see this link -> How to prevent Google Maps API v3 from caching tiles - It works great even though the question / answer has got little attention.