Placing Markers in a Memory Optimized way in Google Map V2 in Android

In a recent project I worked on using Google Map V2 API, I had an issue of loading lots of markers unwantedly (Markers that are there but the user is not seeing them), these consumed lots of extra memory and some times the app crashes.
As a solution I found an alternative way to load the markers that are only available in the visible map area.

GoogleMap = theMap;
LatLngBounds bounds = this.theMap.getProjection().getVisibleRegion().latLngBounds;

LatLng placeLL = new LatLng(yourLatitude, yourLangtitude);

	if (bounds.contains(placeLL)) {
	//Do what you wanted to do with the markers inside the visible area
}

So here using the function of the map object created, we are getting the bounds of the visible map area.
Then checking if our LatLng is inside the bound using the function ‘.contains(Latlang)’.
So now you can place your marker in the map if it is inside the bounds. Thats it.

And a small tip, always try to use a image with low size(in photoshop use ‘save for websites’) and try to recycle the bitmaps after use, if you are using it in the project.
🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s