Using Google Maps V2 for Android with Android Studio


Today we are going to add a map to an Android app using the Google Maps V2, we also use Android Studio instead of Eclipse. The steps to acquire the key are here 

https://developers.google.com/maps/documentation/android/start .

Google Maps for Android

Google Maps for Android (Photo credit: Wikipedia)

Steps

  1. Get an API key from Google
  2. Create a new project
  3. Add a dependency for section to the gradle.build file
  4.  Add permissions to the AndroidManifest.xml file
  5.  Enable OpenGL ES in the AndroidManifest.xml file
  6.  Add meta data section to the AndroidManifest.xmlfile with the API keys
  7.  Add an xml fragment layout file with the declaration for the Google Maps
  8. Use java code to change and manipulate the map
  9. Add markers to the map

Get the Google Maps V2 Android API Key

To get the key for the Google Maps V2 navigate to https://code.google.com/apis/console

Create a new project if you already don’t have one. Navigate to the “Services” section and enable the “Google Maps Android API v2“. Under the project navigate to “API Access” and “Create New Android Key”.

A new window opens with the command to run and retrieve the android debug key on your development machine.

NB : Each android application is signed with a key, by default during development a debug key is used to sign the apps. The key is located under the folder “.android” . In Windows its located under “C:\.android”, in Linux and Mac OS X, the key is located under the home users directory, navigate to your home directory and cd to .android.

Run the keytool command to retrieve the SHA1 key, in my case its

keytool -list -v -keystore debug.keystore

Copy the SHA1 hash together with the package name for the Android application. E.g if the package name for the app was za.peruzal.maps, the we will have

45:B5:E4:6F:36:AD:0A:98:94:B4:02:66:2B:12:17:F2:56:26:A0:E0;za.peruzal.maps

Create a New Project

Start Android Studio and follow the steps to create a new project.

Add a Dependency to The Android Studio build.gradle file. Use the Google APIs as the target for the the project.

Open the project build.gradle file

Add the following line under dependencies :

dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile 'com.google.android.gms:play-services:3.1.36'
}

Click the sync button and wait a little whilst Android Studio downloads the Google Play Services jar files and link them to the project.

Android Permissions to the AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Enable OpenGL ES version 2 in the AndroidManifest.xml file

<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

Add the API key to the AndroidManifest.xml file

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_KEY"/>

Get the key from the Google API console.

Add the Map Fragment
Create a new xml layout file
Add the following within the file

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="14"
map:cameraTilt="45"
android:name="com.google.android.gms.maps.MapFragment"/>

Manipulate the map through Java code
At this point we can run the app on the phone. The emulator does not come with Google Play Services, or it might need to be updated.

In our next post we will use Java code to add markers to the Google Map. Until next time



转载地址:http://peruzal.wordpress.com/2013/08/27/using-google-maps-v2-for-android-with-android-studio/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值