Friday, 27 April 2012

Add Google Map to your App - Part 1

Today, let's see step by step how you can add a Google Map feature to your application.
We will follow these different steps:
1) Download the Google Maps API
2) Include the Google API to your project
3) Get the API key
4) Add a mapview to your layout
5) Modify your AndroidManifest.xml
6) Create a compatible Emulator
7) Retrieve the map on the Java side

Let's start!

1) First you need to download the Google Maps API from the Android SDK Manager:


2) Then include the Google API to your project (right click on your project -> properties)


3) In order to use the Google Maps API, you need to get an API key here
 https://developers.google.com/android/maps-api-signup

You need to provide a MD5 fingerprint of the key you use to sign your application. For debugging purpose, you should get the MD5 fingerprint of your debug key which can be found here :
Windows Vista: C:\Users\\.android\debug.keystore 
Windows XP: C:\Documents and Settings\\.android\debug.keystore 
OS X and Linux: ~/.android/debug.keystore 

This command will give you the fingerprint of your key.
$ keytool -list -alias androiddebugkey -keystore .keystore -storepass android -keypass android 

(When you can use the same command with your release key when you will create a package for release)

This command will return something like that:
Certificate fingerprint (MD5): 94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98 
Just copy the fingerprint and use it on the signup page.

4) Now if you want to add a map to your layout, you just need to insert a MapView tag and replace the value of the android:apiKey attribute with the value of your own key:
     



    

 
5) Don't forget to add the necessary features and permissions to your AndroidManifest.xml.
     

  ...
  
  
  
  
  
    
    ...
  
  ...

6) If you want to test your app on an emulator, you need to create one compatible with Google APIs. To do so, when you create your emulator select Google APIs on the target section :


7) On the Java side, you can now retrieve your map like this :
   
public class MapsActivity extends MapActivity {

 private MapView mapView;
 private List;Geopoint> geoPoints = new ArrayList<Geopoint>();

 @Override
 protected void onCreate(Bundle bundle) {
  super.onCreate(bundle);

  setContentView(R.layout.map);
  
  //Retrieve the map
  mapView = (MapView) findViewById(R.id.mapView);
 }
}
On the next post we will see how you can add annotations or draw lines and poligones on your map.

3 comments:

  1. Thanks for sharing this tips. Where, I have learned about Google maps. You may also learn about some interesting things in Google Maps. How to Styled Maps or Customize colors in Maps? For More info: http://www.labstech.org/styled-maps-google-maps-2014-06-26/

    ReplyDelete

  2. Showbox is one of the best apps among all the others which provide streaming of movies and tv shows.
    The best part is, it is absolutely free for everyone and it also provides multiple resolutions of the videos.
    You can also watch the videos even when you are offline ShowBox for iOS

    ReplyDelete
  3. A Perfect Winning TIP For THAI LOTTERY 3 Digit Up. 2017

    ReplyDelete