Anas Shahid Android Development: Integrating Google Maps in Android application

Tuesday, July 2, 2013

Integrating Google Maps in Android application

So often time in your mobile application you will need to work with location and maps. Here is a simple example how to integrate Google Maps in your android application.

To set up for using maps you will need to first register for google maps Api Key. When I worked on mapsit was quite complicated to create Api key using MD5 fingerprint but now its quite easy.

To register Go to https://code.google.com/apis/console/, and log in with your Google Account.
The following will appear:
Click on the "Create Project" button.
In the list of services, find Google Maps API v3, and click on "off" to turn it on.
In the next screen, check "I Agree..." and the click the "Accept" button. You will now see that the button next to Google Maps API v3 has changed to "on".
Then click "API Access" in the menu to the left. It will ask you to "Create an OAuth 2.0 client id...".
In the next screen, provide a product name (e.g. "demo"), upload an image (if you want to) as a logo of your project, and click the "Next" button.
In the next screen, choose Application type ("Web application"), and type in your web address, and then click the "Create Client Id" button.
In the next screen, you have got an API key.

Remark Note: Save your API key! (You will need it for all Google Maps applications you develop for the particular URL.
Another thing that you will have to do is setting up the build environment. Google maps doesnt work on the normal APIs , it requires to have Google API installed on your emulator. for this go to your SDK manager , check the Google API checkbox and Install the package after you agree the terms of service.

Now create an Emulator having the installed google api. At this point you are done with the hard part of the project.

Create a new project in ADT and set Target SDK Version to the currently installed API.
Copy the following code in XML to access a map. Replace the key with the one you generated from the process above

<com.google.android.maps.MapView
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:id="@+id/routesmap"
                 android:clickable="true"
                 android:apiKey="0L42deK321zB1HWl4KMymkm80kI-bsm0Sy_5chQ"
                 />

In the Java file set the class to extend MapActivity, set the contentview to the xml file having the map and you are done. You can get the id of the map to perform functionalities like setCenter(), setBuiltinZoomControls and displaying markers(which I will show in the next post). Here's some code to help.

public class MainActivity extends MapActivity 
{ private MapView map= null;
private MyLocationOverlay me=null;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map= (MapView) findViewById(R.id.map);
map.getController().setCenter(getpoint(24.860117, 67.026837));
map.setBuiltInZoomControls(true);
map.getController().setZoom(19);
}

private GeoPoint getpoint (double lat, double lon)
{
return (new GeoPoint((int)(lat*1E6), (int)(lon*1E6)));
}
}

PS:Dont forget to put internet permission in your application from manifest file.

Hope now you are able to configure maps correctly.




After you have configured maps correctly you can see How to display Multiple markers on map


View Anas Shahid's LinkedIn profileView Anas Shahid's profile

1 comment:

  1. I want to integrate maps in an already existing project. android:minSdkVersion="14"
    android:targetSdkVersion="17".
    Will I be able to do it with the above SDK?
    Can I change my target SDK?

    ReplyDelete