Thursday, January 12, 2012

Displaying the spinner view in Google Map

For displaying the Google Map or for performing any action in Google Map go to the link:- http://mobiforge.com/developing/story/using-google-maps-android. Now here we are just displaying a spinner view in which there is the names of different countries, and on it, if you click in any country then it will display the map of that country that was clicked. The screenshots are given below for better understand.



Due to the low internet speed it is unable to display the map. Now the code is given below for performing the following operations.

MapViewActivity.java

package com.ex;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class MapViewActivity extends MapActivity implements OnItemSelectedListener {
    MapView mapView;
    Spinner mysp;
    GeoPoint p;
    MapController mc;
    String[] dataSet={"India","Australia","England","Pakistan","South Africa","Sri Lanka"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapView = (MapView) findViewById(R.id.mapView);
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom); 
        View zoomView = mapView.getZoomControls();
       
        zoomLayout.addView(zoomView,
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        mapView.displayZoomControls(true);
        mc = mapView.getController();
        String coordinates[] = {"27.000000", "78.000000"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

        p = new GeoPoint(
            (int) (lat * 1E6),
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(17);
        mapView.invalidate();
        mysp=(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item,dataSet);
        mysp.setAdapter(adapter);
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        switch(arg2){
        case 0:
            showmap(27.000000,78.000000);
            break;
        case 1:
            showmap(35.180000,149.080000);
            break;
        case 2:
            showmap(52.450000,1.300000);
            break;
        case 3:
            showmap(30.000000,70.000000);
            break;
        case 4:
            showmap(29.000000,24.000000);
            break;
        case 5:
            showmap(37.000000,127.300000);
            break;
        default:
            break;
               
        }
       
    }

    private void showmap(Double lat,Double lng) {
        // TODO Auto-generated method stub
           mc = mapView.getController();
          
   
            p = new GeoPoint(
                (int) (lat * 1E6),
                (int) (lng * 1E6));
   
            mc.animateTo(p);
            mc.setZoom(17);
            mapView.invalidate();
       
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
       
    }
}

The layout code is given below:-

main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="04gzvppWXX9dyAixd7QwUv49IJeVZl2Pqy_xfDA"
        />
      <LinearLayout android:id="@+id/zoom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />


      <Spinner
          android:id="@+id/spinner1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true" />

</RelativeLayout>

AndroidMainfest.xml

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="com.google.android.maps" /> 
        <activity
            android:name=".MapViewActivity"
            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>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

Thankyou. Please post your comments for the improvements.