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>


29 comments:

  1. any solution for yesterday am asking

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. I am facinga problem when I try to test this code. The packages and files of are not identified in the first attempt. When I double-clicked on red symbol eclipse is creating the classes inside the project.But no use. were they belong to any google SDK or you craeted those files with your own code?how could I get the fully executable sample?

    ReplyDelete
    Replies
    1. Hi hari..First go through following tutorial

      http://ramsandroid4all.blogspot.in/2013/03/google-maps-android-api-v2.html?showComment=1370426863868

      Delete
    2. nice thank u so much frnd.......

      Delete
  4. i run this, why no display title and icon (.title("my position").icon(BitmapDescriptorFactory............)

    ReplyDelete
  5. how to remove old markers bcoz so many are getting created

    ReplyDelete
    Replies
    1. in onLocationChanged method first call map.clear() method this will remove old marker.

      Delete
  6. in onLocationChanged method first call map.clear() method this will remove old marker.

    ReplyDelete
  7. Syntax mistake here : Toast.makeText(getApplicationContext(),
    location.getLatitude() + "location.getLongitude(),
    Toast.LENGTH_LONG).show();

    ReplyDelete
    Replies
    1. Than Q i noticed that syntax error i will update the post.with following toast

      Toast.makeText(getApplicationContext(),
      location.getLatitude() +", "+location.getLongitude(),
      Toast.LENGTH_LONG).show();

      Delete
  8. This comment has been removed by a blog administrator.

    ReplyDelete
  9. Sorry but in my device Marker don't appear.
    But console signal 0 errors.

    Why?

    ReplyDelete
    Replies
    1. you can add marker programmatically try following method in MarkerOptions class.
      mp. .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));

      Delete
  10. Hi Sir, I need Exact Latitude and Longitude it is Possible or not Using Gps Location Listener

    ReplyDelete
    Replies
    1. yes its possible using GPS .Just change LocationManager.NETWORK_PROVIDER to LocationManager.GPS_PROVIDER

      Delete
  11. Hi Ram, no errors in program however current position is not displayed and also marker does not get displayed on the maps.please advise..

    ReplyDelete
    Replies
    1. I think problem is with the location .your device is not giving current location first of all check whether your able to find current location or not..

      Delete
  12. hi,,,ram...on current position its display..marker..bt not display right location..so how to get a right location??

    ReplyDelete
    Replies
    1. It will display the marker that your device is giving as current location.

      Delete
  13. its not working and i check my device location its show current location whats wrong this plz help..

    ReplyDelete
  14. Thank you so much for your tutorials!!! You have really helped me out, much appreciated.

    ReplyDelete
  15. hii.......
    run correctly on emulator but in device it not show current locationl only show map and not run on 2.3 why

    ReplyDelete
    Replies
    1. may be google play service problem.check in you device that you have google play services apk installed or not..

      Delete
  16. HI do you have any idea about Gameing Concept so that inform to
    mail sathishkumars3@gmail.com

    ReplyDelete
  17. I did your previous map tutorial and its working fine but this tutorial(i have installed it on a device) is not working.the whole map is showing but not my current location(Do i need to do anything with DDMS)

    ReplyDelete