Google Maps on Android

Tutorials with advanced 'difficulty' and more Lines of Code.

Google Maps on Android

Postby saigeethamn » Wed Nov 04, 2009 10:09 am

Maps-based and location-based services are compelling for mobile users. Hence let us explore the support for map services in the Android platform in this tutorial. In the next we will build upon this and add location services too.


Map services are provided by an external library that includes the com.google.android.maps package. This library is not provided as part of the standard SDK. It is provided as a Google APIs add-on to the android SDK. For the convenience of developers, this add-on is provided as part of the SDK on the emulator.


Before I jump into the example, let me explain some fundamental concepts related to the Maps API.


NOTE: For using the Google Maps, you have to obtain a Maps API key. How to obtain the key is also described towards the end of this tutorial. This is Step 1.


Maps API add-on provides a MapActivity class that extends the Activity class of Android. This helps is managing the lifecycle of a MapView object that we will be using later. Our main class in the application will be extending the MapActivity class. This class can be treated as the default activity class whenever we deal with the Maps API.


MapView – This is a class that extends the View and the ViewGroup classes of Android. It provides the basic functionality expected on a Map like responding to key presses or touch and allowing zooming etc. It also provides a controller object that helps us manipulate the Map view programmatically. We will see how, in the example later.


Now that we have understood the basics of the Maps API, let us jump into the example.


Step 2: To begin with, when you create an android project in Eclipse, instead of choosing the target name as Android 1.5, you need to select the target as Google APIs. This ensures that the project has the map API external libraries along with android 1.5 SDK.


Step 3: The Activity class you create this time should not extend the standard “android.app.Activity” class but should extend the MapActivity class about which I explained earlier.


Step 4: Apart from this, you need to add the following tag in the AndroidManifext.xml in order to use the external library:


Code: Select all
<uses-library android:name="com.google.android.maps" />



Step 5: Now let us declare the map view in main.xml in layout folder. To do so, add this element in order to display the map using MapView.

Code: Select all
<com.google.android.maps.MapView
android:id="@+id/myGMap"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:enabled="true"
      android:clickable="true"
      android:apiKey="0YaGLMeMFKXrhzHL-uSYZSnqXqbGwE6kxF4VwFQ"
    />


Here the apikey should be what was generated by you in an earlier step 1.


Step 6: In the activity class I set the geo point on the map and set the satellite view to false.

Code: Select all
      myMapView = (MapView) findViewById(R.id.myGMap);
      geoPoint = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
      myMapView.setSatellite(false);


Step 7: Now you are almost ready to go. But we will try to do some programtic handling of the mapview. For that we need to get a handle to the MapController object. It is done this way:

Code: Select all
      myMC = myMapView.getController();
      myMC.setCenter(geoPoint);
      myMC.setZoom(15);

I also set the zoom controls to be displayed with this code.

Code: Select all
      myMapView.setBuiltInZoomControls(true);
      myMapView.displayZoomControls(true);


Step 8: Add the following permissions in the AndroidManifest.xml in order to access the maps over the internet:

Code: Select all
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>


You could execute your program now to view the map with the fixed geopoint.


Step 9: One last step – let us add some actions to the key presses for zooming in, zooming out, viewing the satellite view, normal map view

Code: Select all
      public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_I) {
                  myMapView.getController().setZoom(myMapView.getZoomLevel() + 1);
                  return true;
            } else if (keyCode == KeyEvent.KEYCODE_O) {
                  myMapView.getController().setZoom(myMapView.getZoomLevel() - 1);
                  return true;
            } else if (keyCode == KeyEvent.KEYCODE_S) {
                  myMapView.setSatellite(true);
                  return true;
            } else if (keyCode == KeyEvent.KEYCODE_M) {
                  myMapView.setSatellite(false);
                  return true;
            }
            return false;
      }


Now you can run the applications. The Google map will show up with a pointer to the geo location defined in the program. You can click on I, O, S and M for zooming in, zooming out, viewing the satellite map or the normal map respectively.


Interesting?


Obtaining a Maps API Key:


Step 1: Locate debug.keystore on your system. It is usually in the USER_HOMELocal SettingsApplication Data.android folder on windows.


Step 2: Use the keytool utility to generate certificate fingerprint (MD5). keytool utility that comes with the default JDK installation.


C:>keytool -list -alias androiddebugkey -keys
tore .androiddebug.keystore -storepass android -keypass android


It will getnerate a certificate fingerprint (MD5) in this format:


Certificate fingerprint (MD5): XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX


Step 3: Go to the page for obtaining the Maps API Key. Put your Certificate fingerprint (MD5) And get your API key for android Google Map application. On this page, paste the certificate fingerprint in the appropriate form field, and click the “Generate API Key” button. This will return a Maps API key string.


This key needs to be used along with the MapView element declared in the XML layout file as mentioned in the tutorial above.

NOTE: For many more such tutorials please visit http://saigeethamn.blogspot.com
Attachments
SampleMapApp.zip
(25.52 KiB) Downloaded 897 times
A person who loves to share my ten cents of knowledge
http://saigeethamn.blogspot.com
saigeethamn
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Aug 26, 2009 10:51 am

Top

Problem with displaying map on MapView

Postby pallavi_vispute » Sat Feb 13, 2010 2:16 pm

Hi,
I refered sample code from u r site SampleMapAcivity but its not working.I follow all the steps also got an API-key but still i can't get map. Its very urgent to me to display map so pls give me reply soon.
Attachments
SamplmapApp.rar
Hi, this is the whole source which i am using but i can't get result.
(26.16 KiB) Downloaded 198 times
op for map.doc
Hi, this attachment is for wht o/p i got.
(76 KiB) Downloaded 228 times
pallavi_vispute
Freshman
Freshman
 
Posts: 2
Joined: Sat Feb 13, 2010 1:06 pm

Re: Google Maps on Android

Postby endhck » Sun Jul 18, 2010 11:21 am

I did the same thing with you but I can not see the map and instead of map there is something like design paper. Is there anything I must do?
endhck
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Jul 06, 2010 3:20 pm

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests