Wednesday, 5 June 2013

Google Maps Android API V2 showing current location with marker(where am i in google android maps api v2)

Download this projectGoogleMapsCurrentLocationWithMarker.zip

you can view the tutorial of displaying google maps android api v2  from my previous tutorial http://ramsandroid4all.blogspot.in/2013/03/google-maps-android-api-v2.html?showComment=1370426863868

and finding current location tutorial here : http://ramsandroid4all.blogspot.in/2013/01/finding-current-location-using.html

In this tutorial i'm going show you how to display our current location in google maps android api v2 with help of marker .

To understand this tutorial you should know how to display map and how to find out current location these concepts you can learn from above links.

ScreenShot:
activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:map="http://schemas.android.com/apk/res-auto"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <fragment        android:id="@+id/map"        android:name="com.google.android.gms.maps.MapFragment"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>



MainActivity.java:
package com.ram.ammapsv2;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements LocationListener {
GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();

}

@Override
public void onLocationChanged(Location location) {

map.clear();

MarkerOptions mp = new MarkerOptions();

mp.position(new LatLng(location.getLatitude(), location.getLongitude()));

mp.title("my position");

map.addMarker(mp);

map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

}



AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.ram.ammapsv2"    android:versionCode="1"    android:versionName="1.0" >
    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="19" />
    <uses-feature        android:glEsVersion="0x00020000"        android:required="true" />
    <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" />    <!--     The following two permissions are not required to use     Google Maps Android API v2, but are recommended.    -->    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <permission        android:name="com.ram.ammapsv2.permission.MAPS_RECEIVE"        android:protectionLevel="signature" />
    <uses-permission android:name="com.ram.ammapsv2.permission.MAPS_RECEIVE" />
    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.ram.ammapsv2.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.gms.version"            android:value="@integer/google_play_services_version" />        <meta-data            android:name="com.google.android.maps.v2.API_KEY"            android:value="paste your api key here" />    </application>
</manifest>