Home > Uncategorized > Using Google maps in an Android app

Using Google maps in an Android app

Although you can find many tutorials/walkthroughs for this one it took a lot of time for me until I got it working (which is actually the difficult part). Instead of repeating what all those tutorials say I will go over the parts which took my time the most:

  • First of all, you need to get the Google APIs for maps to work. After you download the ones you want using the SDK manager, right click on you project then go to properties->Android and choose the Google API that you have downloaded as your target.
  • Your activity needs to extend MapActivity instead of Activity
  • Almost all of the tutorials that you will find creats just a MapActivity and thus they keep talking about an XML snippet that you need to add somewhere called Main.xml . Well if you want to add this to your own on going project and you are a  newbie to Android (like me (somewhat)) then this will probably confuse you. You need to add that snippet which is shown below to the XML file of your Layout (in my case I erased everything and added that)
    <!--?xml version="1.0" encoding="utf-8"?-->
    
    

    Note the android:apiKey attribute which I will talk about later. You should also in your Manifest.xml file as a child of the <application> (which means between the <application> </application> tags):

    <uses-library android:required="true" 
          android:name="com.google.android.maps"/>
    

    and

    <activity
    	android:name="org.APPLICATION.LocationActivity"
    	android:label="@string/title_activity_map" 
    	android:theme="@android:style/Theme.NoTitleBar">
    </activity>
    

    in addition to the permission

    <uses-permission android:name="android.permission.INTERNET" />
    
  • (not that you may need more such as ACCESS_COARSE_LOCATION but I am not sure which ones exactly)

  • So if you have done everything right until here (and if I did not skip anything) you should be able to see a <em>grid</em> but you will notice that it doesn’t show the map even if you wait for a very long time (thinking that internet is slow). The reason is that you need an API key for Maps to work. Refer here  for getting an MD5 finger print. Once you get that go here  to get your key and use it as the value of the attribute android:apiKey
  • After you do all these (phew) the code you find on the tutorial sites should work for you. One thing you need to be careful is that  the GeoPoint takes integer microdegrees as argument so you need to do (degrees * 1E6) and cast to int (if it is  not)
  • Finally go get your self a nice pin icon from here and use it as a drawable to get a location idicator on the map.

Here is what I have used as code to get the map:


public class LocationActivity extends MapActivity {

	private List mapOverlays;
	private Drawable drawable;
	private MyOverlays itemizedOverlay;

  public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_map); // bind the layout to the activity

    MapView mapView;
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapOverlays = mapView.getOverlays();
    drawable =  this.getResources().getDrawable(R.drawable.pin);
    itemizedOverlay = new MyOverlays(drawable);

    GeoPoint point = new GeoPoint((int)(Double.parseDouble(GpsData.latitude) * 1e6),
    		(int)(Double.parseDouble(GpsData.longitude) * 1e6));

    itemizedOverlay.addItem(point, "", "");
    mapOverlays.add(itemizedOverlay);

    MapController mc = mapView.getController();

    //Set the initial zoom level
    mc.setZoom(15);

    //Center the location indicator
    mc.setCenter(point);

  }

  @Override
  protected boolean isRouteDisplayed() {
    return false;
  }

}

and

public class MyOverlays extends ItemizedOverlay {

	private ArrayList overlayItemList = new ArrayList();

	public MyOverlays(Drawable marker) {

	    super(boundCenterBottom(marker));
	    populate();
	}

	public void addItem(GeoPoint p, String title, String snippet) {
	    OverlayItem newItem = new OverlayItem(p, title, snippet);
	    overlayItemList.add(newItem);
	    populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
	    return overlayItemList.get(i);
	}

	@Override
	public int size() {
	    return overlayItemList.size();
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
	    super.draw(canvas, mapView, shadow);
	}
}

References:

https://developers.google.com/maps/documentation/android/hello-mapview

http://www.vogella.com/articles/AndroidGoogleMaps/article.html

http://stackoverflow.com/questions/9395336/android-draw-pin-on-google-map

A nice Turkish tutorial: http://www.ahmetcebisli.net/2011/07/android-mapview-uygulamalari/

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment