Integrate Google Map In Android – An Easy Guide

How to start

  • Google provides library map api which is available in “com.google.android.maps”  package to user Google map in Android application.
  • To use google map you need additional map. You can get map key from here
  • You have to add uses-library tag(<uses-library android:name=”com.google.android.maps”/>)in your AndroidManifest.xml

Create Project

  • Create new project “com.spec.demo” with activity GoogleMapDemoActivity. Select Google API as build target(see figure1).

Integrate Google Map In Android – An Easy Guide

  • You need to add Google maps library and Internet permission to your application. Add uses-library to “AndroidManifest.xml”(see Figure2).

code sample

  • Now change the “main.xml” layout file to following.

Replace “Your Maps API Key” with your Google API key.

<?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 Maps API Key”/>

</LinearLayout>

  • Create following activity. This activity will use LocationListner to update location in map.

package com.spec.demo;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GoogleMapExampleActivity extends MapActivity {
/** Called when the activity is first created. */
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;

public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);

mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapController = mapView.getController();
mapController.setZoom(14);
locationManager = (LocationManager)                                       getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new GeoUpdateHandler());
}

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

public class GeoUpdateHandler implements LocationListener {

@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
mapController.animateTo(point); //    mapController.setCenter(point);
}

@Override
public void onProviderDisabled(String provider) {}

@Override
public void onProviderEnabled(String provider) {}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
}

Run Application

  • To run map app you need to create emulator with Google API(see figure3).

integrate-google-map-in-android2

  • Now build and run app. A map with your location is appeared in your screen shown in Figure 4.. Please make sure that you need to run the application in Android device.

integrate-google-map-in-android3

Thank you

Tags: , , , , , , ,

Comments are closed.