Android Tutorial: integrating Google Map V2 in an Android app with Android Studio

Integrating Google Map V2 in an Android 

If you want to include Google Maps V2 in your Android  app using Android Studio,you must follow  steps below:

1. In your SDK Manager, make sure that the Google Play Services is installed.

Android SDK Manager

2. To get your MAP key   from Google console, you must have the SHA1 key to your IDE (Android Studio). You must use the following command in your command prompt:

(this command line must be executed in the" /bin" folder of the JDK used by Android Studio).
 keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
Note the SHA1 key provided. It is needed later.

3. Create a new project.

4. Now you have to get a free API key to your application. Sign in Google Developers Console API: 

https://console.developers.google.com. Click the Create Project button. Next, you need  enabling   Google Maps v2 API for Android  Click afterwards on "credentials" that is under the API and authentication menu at the top left. Click add credentials (choose API key) and click on Android key. Finally, click Add name of the package and  so you must paste the name of the package in the area that is dedicated to it and the same for the SHA1 key. Next, click Create. This will generate an API key that you will use in your Android application.

5. In your module build.gradle, insert the following in the dependencies (see picture bellow):



Android build.gradle GoogleMap Gagle Api
1
2
3
4
5
dependencies {
    ...
    compile 'com.google.android.gms:play-services-maps:6.5.87'
    ...
}
Synchronize the project thereafter. (Sync project) 6. Subsequently you must add the required permissions in your AndroidManifest.xml Note that you must put the name of your package instead of "yourPackageName". (Permissions must be outside of the Application tag ).
<permission android:name="yourPackageName.permission.MAPS_RECEIVE" />
     <uses-permission android:name="yourPackageName.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

You should also add the following tag at the Application tag. You have to put your key generated by Google Developer Console API instead of generated_api_key_by_google.
<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="generated_api_key_by_google"/>

You have to put this code into your xml layout :

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
    android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>

Run your application, you will get this (you can not test the app on the emulator):


GoogleMap Android

Commentaires

Posts les plus consultés de ce blog

Premier tutoriel avec Arduino et Proteus ISIS

Login et password ,formulaire d'authentification sous android avec mysql et json

Connexion d'une application Android et une base de données externe Mysql avec web services php Json (opérations CRUD)