Monday, 13 May 2013

Google Maps API V2 Tutorial, Steps to get Maps in Android

Create New project and Do this 3 setps for getting Google Maps
1) Download & Configure Google Play Services.
2) Get Google Maps API V2 key.
3) Your Project add some additional code.

1) Download & Configure Google Play Services:
If you want to develop Google Maps Application you must download "Google Play Services" library and configure to work with your existing Android SDK.
Download:
Window => Android SDK Manager => Extras => Click Google Play Services => click Download.

Import to Workspace:
File => Import => Android => Existing Code into Workspaces
then browse to the location of the downloaded "Google Play Sevices" package on your computer.
Open your ADT Bundle=>
sdk/extras/google/google_play_services/libproject/google_play_services_lib
click ok => click finish button.

Add Project:
right click on your project => properties => android => In Library pane click Add button => select google_play_services => click apply =>click OK button.

Add Support Library:
right click on your project => Anroid Tools => click Add Support Library.

2) Get Google Maps API V2 key:

If you want to get Google Maps API V2 key we need System SHA1 key.

Get SHA1 Key:
Open Command Prompt, paste your "your_user_name" into below Command and Run.

keytool -list -v -keystore "C:\Documents and Settings\your_user_name\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

Then u get SHA1 Key

Get Google Maps API V2 Key:
Open Google API Console (code.google.com/apis/console)
i)click services and activate "Google Maps API V2"
ii)click API Access pane click "create new Android key" and give your_SHA1_key;your_project_package_name
( Goto your manifest.xml and take package ex: package="your_project_pcakge_name")

Then click create button, Google Maps API V2 key will be generated.

3) In Your Project add some additional code

i)In your manifest.xml:
We need some permissions, feature and Meta Tag

Meta Tag:
Add this meta tag in your before closing </application> tag in manifest.
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Write_ur_Google_Maps_API_Key" />

Note: Write your Google Maps API key in meta tag ( previous step you got key)

Permissions:
    <permission
        android:name="replace_ur_package_name.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
<uses-permission android:name="replace_ur_package_name.permission.MAPS_RECEIVE"/>

Also add these permissons:
<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" />
<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"/>

Feature:

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

ii)In your layout activity_main.xml file:

Add this code
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Here i use SupportMapFragement because its support All versions

iii) Your Class Must extends with FragmentActivity and add this code also
SupportMapFragment smf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap map = smf.getMap();

Run Your Project into device you got a Map like this way

 

Show Current Location:
// enable current location
map.setMyLocationEnabled(true);

Add Marker:

double lati = 17.388321, longi = 78.487869;
       LatLng position = new LatLng(lati, longi);

// Add marker to map
Marker marker = map.addMarker(new MarkerOptions()
                  .title("Hyderabad")
                  .icon(BitmapDescriptorFactory
                       .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                  .position(position));

Move to Marker position:
// move camera to position
map.moveCamera(CameraUpdateFactory.newLatLng(position));

Zoom Marker position:
// zoom controls
map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 10));

Add Listener for Marker:
// add listener for marker
map.setOnMarkerClickListener(new OnMarkerClickListener(){

@Override
public boolean onMarkerClick(Marker marker) {
            Toast.makeText(ctx, "U clicked Markar", 3000).show();
            Intent it = new Intent(Intent.ACTION_VIEW);
            it.setData(Uri.parse("http://venkoolit.blogspot.com"));
            startActivity(it);
            return false;
            }
       });

Add Polyline:
map.addPolyline(new PolylineOptions().add(
       new LatLng(17.498211000000000000, 78.409864799999920000),
       new LatLng(17.346480600000000000, 78.550958300000050000))
       .geodesic(true));

Show Traffic:
// enable trafic
map.setTrafficEnabled(true);

Change Map Type:
// set map type satellite
map.setMapType(map.MAP_TYPE_SATELLITE);

Google Maps Demo Click Here

Then you got a Map like this way

3 comments:

  1. Use
    your_System_user_name insted of your_user_name (In Generating SHA1 key)
    and
    activate
    Google Maps Android API V2 insted of Google Maps API V2

    ReplyDelete
  2. Latest google_play_services_lib library contains some extra tag

    So we need add extra tag in your manifest.xml

    That is

    meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"

    plz see this link
    https://developers.google.com/maps/documentation/android/start#add_the_google_play_services_version_to_your_apps_manifest

    ReplyDelete
  3. Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks. 상품권 매입

    ReplyDelete