Eric H
Eric H

Reputation: 1676

How to reuse / reinitialize a gmap3 (Version 5) created google map?

I want to reuse a google map that I created with gmap3 after I initialized it once with a set of markers. This currently works, however I explain later

Currently I run something similar to:

$('#mapWrapper').gmap3({
    clear: {
        name:['marker', 'infoWindow']
    },
    map:{
        options: mapOptions
    },
    marker:{
        values: markers
    }
});

When I want to update the map with new markers I run the same piece of code.

I am having issues running in a hybrid app on ios7 with cordova, due to memory limits with the new ios7. So I am looking on ways to remove memory leaks.

Upvotes: 0

Views: 1545

Answers (1)

davidkonrad
davidkonrad

Reputation: 85528

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.

Upvotes: 1

Related Questions