Saturday, July 13, 2013

Working With Google Map in Android

 First configure the sdk and import google play service


 install google play service from the sdk first
then import it to your workspace
add the imported library to your application
 choose any of the google APIs for your application





First extend the FragmentActivity class in your activity
public class MainActivity extends FragmentActivity


Declare these functions

     public GoogleMap mapView;
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mapView == null) {
            // Try to obtain the map from the SupportMapFragment.
            mapView = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview)).getMap();
            // Check if we were successful in obtaining the map.
            if (mapView != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        // Hide the zoom controls as the button panel will cover it.
        mapView.getUiSettings().setZoomControlsEnabled(true);
        mapView.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }

and call the function

        setUpMapIfNeeded(); inside onCreate function





this on your layout xml
           
 <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
      android:id="@+id/mapview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"/>
    </FrameLayout>


this on your manifest file



<permission
    android:name="com.vivek.gpstrack.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
  <uses-permission android:name="com.vivek.gpstrack.permission.MAPS_RECEIVE"/>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
  <!-- External storage for caching. -->
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <!-- My Location -->
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <!-- Maps API needs OpenGL ES 2.0. -->
  <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>



    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />
    <meta-data
      android:name="com.google.android.maps.v2.API_KEY"
      android:value="use your key here"/>

        <activity
            android:name="com.vivek.gpstrack.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

No comments:

Post a Comment