Sunday, December 6, 2015

google maps android api v2 tutorial using android studio

If you want to include Google Maps in your Android app and you are 
using Android Studio, just follow the simple steps below :
1. In your SDK Manager, make sure that the Google Play Services is installed.
01
2. Next is to get the SHA1 key needed for the registration of the app to Google’s Developer Console.
In the command line, issue the following command for Windows:
1
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
I am not sure what it does, I just read it from the documentation. For other operation systems, check the documentation (see the debug certificate). If you cannot find the keytool program, it is located under the bin directory of your JDK folder.
Take note of the SHA1 key provided. This will be needed in the next step.
3. Get a free API key for your app. Sign up in Google APIs Console. Click on the Create Project button. Afterwards, go to the APIs & auth. Enable the Google Maps Android API v2. Click on the Credentials then Create new Key under the Public API access. Choose Android key.
4. Enter the SHA1 key generated in step 3. Followed by semicolon, then the package of your Android app.
05
After, click on the Create. This will generate an API key which you shall use in your Android app. You should enter a different key if yourelease your app. This is the SHA1 for the debug key only.
5. In your module build.gradle, insert the following in the dependencies:
1
2
3
4
5
dependencies {
    ...
    compile 'com.google.android.gms:play-services-maps:6.5.87'
    ...
}
Sync the project afterwards. Latest version can be checked here.
6. In the AndroidManifest.xml, insert the following:
Notice that the name specified in the permission tag in lines 5-6 should be the same with line 8. Use the name of the package of your app followed by permission.MAPS_RECEIVE.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
    package="net.yancyparedes.googlemapsproject" >
 
    <permission android:name="net.yancyparedes.googlemapsproject.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
 
    <uses-permission android:name="net.yancyparedes.googlemapsproject.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"/>
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
 
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="generated_api_key_by_google"/>
    </application>
 
</manifest>
7. Next is to insert a fragment inside the Activity XML.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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>
8. Next step is to put in the Java codes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.googlemapsproject;
 
import android.graphics.Camera;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
 
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
 
public class MainActivity extends ActionBarActivity {
 
    private static final LatLng DAVAO = new LatLng(7.0722, 125.6131);
    private GoogleMap map;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
 
        Marker davao = map.addMarker(new MarkerOptions().position(DAVAO).title("Davao City").snippet("Ateneo de Davao University"));
 
        // zoom in the camera to Davao city
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(DAVAO, 15));
 
        // animate the zoom process
        map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
    }
 
    ...
}
9. Test your application. Unfortunately you cannot use an emulator. You have to test your app in an actual phone. There is a workaround but I will not cover it anymore.
2015-03-04 12.26.09
This is a screen shot of the app I created.

No comments:

Post a Comment