Google Maps Android API v2 - Tutorial
1. Google Maps
Google provides via Google play a
library for using Google Maps
in your application. The following description is based on the Google
Maps Android API v2 which provides significant improvements to the
older API version.
The library provides the
You need to add additional information to your
The library provides the
com.google.android.gms.maps.MapFragment
class and the
MapView
class for displaying the map component.
You need to add additional information to your
AndroidManifest.xml
file to use Google Maps.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.vogella.android.locationapi.maps" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.sample.android.locationapi.maps.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="your_apikey" /> </application> </manifest>
The
the
MapFragment
class
extends the
Fragment
class and provides the
life-cycle management and the services for
displaying a
GoogleMap
widget.
GoogleMap
is the class which shows the map. The
MapFragment
has the
getMap()
method to access this class.
the
LatLng
class can be used to interact with the
GoogleView
class.
You can create markers on the map via the
The following code shows an example.
On the
Similar to you also listen to drag events and info window clicks.
Marker
class. This class can be highly customized.
The following code shows an example.
public class MainActivity extends Activity { static final LatLng HAMBURG = new LatLng(53.558, 9.927); static final LatLng KIEL = new LatLng(53.551, 9.993); 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(); if (map!==null){ Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG) .title("Hamburg")); Marker kiel = map.addMarker(new MarkerOptions() .position(KIEL) .title("Kiel") .snippet("Kiel is cool") .icon(BitmapDescriptorFactory .fromResource(R.drawable.ic_launcher))); } }
On the
GoogleMap
you can register a listener for the
markers in your map via the
setOnMarkerClickListener(OnMarkerClickListener)
method. The
OnMarkerClickListener
class defines the
onMarkerClicked(Marker)
method which is called if a marker is clicked.
Similar to you also listen to drag events and info window clicks.
The
The following example code is taken from the offical Google webpage.
GoogleMap
can be highly customized.
The following example code is taken from the offical Google webpage.
static final LatLng HAMBURG = new LatLng(53.558, 9.927); static final LatLng KIEL = new LatLng(53.551, 9.993); private GoogleMap map; ... // Obtain the map from a MapFragment or MapView. //Move the camera instantly to hamburg with a zoom of 15. map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15)); // Zoom in, animating the camera. map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
Unfortunately for using the Google Maps you have to test the
application on a real device as the emulator is not supported.
Open the
Android SDK Manager
and install
→
Import the library which was downloaded into Eclipse via → → → .
To use this library define a library dependency in your Android project.
Import the library which was downloaded into Eclipse via → → → .
To use this library define a library dependency in your Android project.
To use Google Maps you need to create a valid Google Maps API
key. The key is free, you can use it with any of your applications
that call the Maps API, and it supports an unlimited number of
users.
You get this key via the so-called Google APIS Console. You have to provide your application signature key and the application package name.
This is based on the key with which you sign your Android application during deployment. During development with Eclipse, Eclipse us automatically creates and uses a debug key.
You get this key via the so-called Google APIS Console. You have to provide your application signature key and the application package name.
This is based on the key with which you sign your Android application during deployment. During development with Eclipse, Eclipse us automatically creates and uses a debug key.
The Eclipse debug
key for signing your application can be found in the
To create the SHA-1 for your debug keystore you use the
Copy the SHA-1 output, as you need this later.
userhome/.android/debug.keystore
file.
To create the SHA-1 for your debug keystore you use the
keytool
command from your JDK
installation pointing to the
debug.keystore
file.
keytool -list -v -alias androiddebugkey \ -keystore <path_to_debug_keystore>debug.keystore \ -storepass android -keypass android
Copy the SHA-1 output, as you need this later.
You have to register in
the
Google APIs Console
that you want to use Google Maps for Android. You can reach this
console via the following link:
Google APIs Console
. Select here the
Services
entry.
Activate the Google Maps Android API v2.
Activate the Google Maps Android API v2.
You need later to register your application via its package in
this
console together with the SHA-1 fingerprint of your signature
key. For
this you select the entry and click on the
API Access
entry. Afterwards click on the
entry.
Enter your SHA-1 fingerprint and the package of your application separated by a semicolon. For example you can use the
The procedure is described in detail in the following link: Getting a Google Maps key .
Enter your SHA-1 fingerprint and the package of your application separated by a semicolon. For example you can use the
com.vogella.android.locationapi.maps
package.
The procedure is described in detail in the following link: Getting a Google Maps key .
In the following chapter we will build an Android application which
shows a
Create a new Android project called com.vogella.android.locationapi.maps with an
Change the
Get a valid API key for your application and enter this key in the
GoogleMap
.
Create a new Android project called com.vogella.android.locationapi.maps with an
Activity
called
ShowMapActivity.
Change the
AndroidManifest.xml
file
to the following code.
Add the following permissions to your
application.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sample.android.locationapi.maps" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.sample.android.locationapi.maps.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="your_apikey" /> </application> </manifest>
Get a valid API key for your application and enter this key in the
AndroidManifest.xml
file.
In this example we use the
MapFragment
. Change your layout file to the following code.
<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" 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>
Change your
Activity
to the following.
package com.sample.android.locationapi.maps; import android.app.Activity; import android.os.Bundle; import android.view.Menu; 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.BitmapDescriptorFactory; 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 Activity { static final LatLng HAMBURG = new LatLng(53.558, 9.927); static final LatLng KIEL = new LatLng(53.551, 9.993); 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 hamburg = map.addMarker(new MarkerOptions().position(HAMBURG) .title("Hamburg")); Marker kiel = map.addMarker(new MarkerOptions() .position(KIEL) .title("Kiel") .snippet("Kiel is cool") .icon(BitmapDescriptorFactory .fromResource(R.drawable.ic_launcher))); // Move the camera instantly to hamburg with a zoom of 15. map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15)); // Zoom in, animating the camera. map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
Comments
Post a Comment