If your objects which you want to show on the Google Maps in Android have different markers. You need to use lazy loading for markers.

How to do it? It’s very easy. Just use AsyncTask . 

Below you can download source code of Android project.

When you start application you can see Google Maps with red default markers. When custom marker loaded the custom marker will replace default marker and update map overlay.

To see map you need specify your own Google Maps API Key in MapView  (see main.xml file)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<com.google.android.maps.MapView 
		android:id="@+id/mapView" 
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent"
		android:clickable="true" 
		android:apiKey="YOUR_API_KEY" />
</LinearLayout>

Other info you can see in the code. It is simple to understand. Please, add the questions in comments.

Here code of the AsyncTask class

package com.hrupin.lazymarkers;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;

public class MapOverlayItemMarkerAsyncTask extends AsyncTask<String, Void, Bitmap> {

	private OverlayItem overlayitem;
	private MapView mapView;

	public MapOverlayItemMarkerAsyncTask(OverlayItem overlayitem, MapView mapView) {
		this.overlayitem = overlayitem;	
		this.mapView = mapView;
	}
	
	@Override
	protected Bitmap doInBackground(String... params) {		
		Bitmap bitmap = null;
		InputStream in = null;
		try {
			in = fetch(params[0]);
			bitmap = BitmapFactory.decodeStream(in, null, null);
			in.close();
			return bitmap;
		} catch (IOException e1) {
			return null;
		} 
	}

	private static InputStream fetch(String address) throws MalformedURLException,IOException {
	    HttpGet httpRequest = new HttpGet(URI.create(address) );
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
	    HttpEntity entity = response.getEntity();
	    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
	    InputStream instream = bufHttpEntity.getContent();
	    return instream;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		if (result != null) {
			Drawable drawable = new BitmapDrawable(result);
			drawable.setBounds(-drawable.getIntrinsicWidth() / 2, -drawable.getIntrinsicHeight(), drawable.getIntrinsicWidth() / 2, 0);
			overlayitem.setMarker(drawable);
			mapView.invalidate();
		}
	}
	
}

Here you can download the source code of the Android project.

Download it from github

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.