Wednesday, July 31, 2013

Android Google Map V2




To use the Google Maps Android API v2 in your app, you will first need to install the Google Play services SDK.

Install Google Play Service

Open Android SDK Manager and install Google Play services from Extras


Obtain Google Map V2 Key

Generate SHA-1 key of debug.keystore or application sign keystore.

Debug keystore can be found at <userhome/.android/debug.keystore>

To generate SHA-1 of debug keystore use below command :
keytool -list -v -keystore <debug keystore path>/debug.keystore -alias androiddebugkey -storepass android -keypass android

To access the Google Maps with the Maps API, you have to add a Maps API key to your application. You can get this key via Google API Console .

Create your project in Google API Console



Create new Android Key


Enter your SHA-1 key;your application package name


Use API key in your manifest file.



Activate Google Maps Android API v2 from Services

Example :

Import Google play Services library into your workspace.

Import from <android sdk>/extras/google/google_play_services/libproject/google-play-services_lib

Create your project
Add Google Play Service library into your Project.

In your project Manifest file, include below code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ngshah.googlemapv2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.ngshah.googlemapv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.ngshah.googlemapv2.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" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="your_api_key" />

        <activity
            android:name="com.ngshah.googlemapv2.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>
    </application>

</manifest>
Change your api key.
<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

android:glEsVersion="0x00020000" means its supports only OpenGL ES version 2 devices. And this tag should be included in your Manifest file. 
In your xml file write below fragment.
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

Add Marker on Map

package com.ngshah.googlemapv2;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
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 FragmentActivity {
    
    private GoogleMap googleMap;
    private final LatLng HAMBURG = new LatLng(53.558, 9.927);
    private final LatLng KIEL = new LatLng(53.551, 9.993);
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        
        if ( googleMap != null ) {
              Marker hamburg = googleMap.addMarker(new MarkerOptions().position(HAMBURG)
                        .title("Hamburg"));
              Marker kiel = googleMap.addMarker(new MarkerOptions()
                        .position(KIEL)
                        .title("Kiel")
                        .snippet("Kiel is cool")
                        .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.ic_launcher)));

              googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
              googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
        }
    }
}

Download full Source Code

1 comment:

  1. Google Map Service Provider

    Want to get Google Map Service Provider in Ahmedabad? Then Mart2Global is the complete best solution Company. Mart2global is a premier SEO, SMO, Social Media and Paid Ads Marketing Company in Ahmedabad, India, which provides complete Digital Marketing solution.

    ReplyDelete