Tuesday 12 November 2013

Google Map Android API V2 – Switching between Normal View, Satellite View and Terrain View

Step 1 :- Create New Android Project.



Step 2 :- Add Google-play-services_lib to your project.

Step 3 :- Open AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapv2.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
   
    <permission
          android:name="com.mapv2.demo.permission.MAPS_RECEIVE"
          android:protectionLevel="signature"/>
   
    <uses-permission android:name="com.mapv2.demo.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" >
        <activity
            android:name="com.mapv2.demo.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="AIzaSyCmX7SLVHXxU9pSqb2QbAOvdnjAGUulOrk"/>
       
    </application>

</manifest>

Step 4 :- Open activity_main.xml.

<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" >
   
   
    <RadioGroup
        android:id="@+id/rg_views"
        android:orientation="horizontal"   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"   
        >       
       
        <RadioButton
            android:id="@+id/rb_normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_rb_normal" 
            android:checked="true"         
            />
       
        <RadioButton
            android:id="@+id/rb_satellite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_rb_satellite"                       
            />
       
        <RadioButton
            android:id="@+id/rb_terrain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_rb_terrain"                       
            />
       
       
    </RadioGroup>
   

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_below="@id/rg_views"
          class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>
Step 5 :- Open MainActivity.java

package com.mapv2.demo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends FragmentActivity {

    GoogleMap googleMap;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        googleMap = mapFragment.getMap();
       
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
       
        RadioGroup rgViews = (RadioGroup) findViewById(R.id.rg_views);
       
        rgViews.setOnCheckedChangeListener(new OnCheckedChangeListener() {
           
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.rb_normal){
                    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                }else if(checkedId == R.id.rb_satellite){
                    googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                }else if(checkedId == R.id.rb_terrain){
                    googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                }
               
            }
        });
       
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
   
}

Step 6 :- Run Code.




Step 7 :- Download Code :- https://dl.dropboxusercontent.com/u/109954727/Map/map6.zip


No comments:

Post a Comment