Download this project : GoogleMapsCurrentLocationWithMarker.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:
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" >
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:
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:
any solution for yesterday am asking
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteI 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?
ReplyDeleteHi hari..First go through following tutorial
Deletehttp://ramsandroid4all.blogspot.in/2013/03/google-maps-android-api-v2.html?showComment=1370426863868
nice thank u so much frnd.......
Deletei run this, why no display title and icon (.title("my position").icon(BitmapDescriptorFactory............)
ReplyDeletehow to remove old markers bcoz so many are getting created
ReplyDeletein onLocationChanged method first call map.clear() method this will remove old marker.
Deletein onLocationChanged method first call map.clear() method this will remove old marker.
ReplyDeleteSyntax mistake here : Toast.makeText(getApplicationContext(),
ReplyDeletelocation.getLatitude() + "location.getLongitude(),
Toast.LENGTH_LONG).show();
Than Q i noticed that syntax error i will update the post.with following toast
DeleteToast.makeText(getApplicationContext(),
location.getLatitude() +", "+location.getLongitude(),
Toast.LENGTH_LONG).show();
This comment has been removed by the author.
ReplyDeleteyes, it is possitble
DeleteSorry but in my device Marker don't appear.
ReplyDeleteBut console signal 0 errors.
Why?
you can add marker programmatically try following method in MarkerOptions class.
Deletemp. .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
Hi Sir, I need Exact Latitude and Longitude it is Possible or not Using Gps Location Listener
ReplyDeleteyes its possible using GPS .Just change LocationManager.NETWORK_PROVIDER to LocationManager.GPS_PROVIDER
DeleteHi Ram, no errors in program however current position is not displayed and also marker does not get displayed on the maps.please advise..
ReplyDeleteI 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..
Deletehi,,,ram...on current position its display..marker..bt not display right location..so how to get a right location??
ReplyDeleteIt will display the marker that your device is giving as current location.
Deleteits not working and i check my device location its show current location whats wrong this plz help..
ReplyDeleteThank you so much for your tutorials!!! You have really helped me out, much appreciated.
ReplyDeleteThan Q Kevin
Deletehii.......
ReplyDeleterun correctly on emulator but in device it not show current locationl only show map and not run on 2.3 why
may be google play service problem.check in you device that you have google play services apk installed or not..
DeleteHI do you have any idea about Gameing Concept so that inform to
ReplyDeletemail sathishkumars3@gmail.com
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)
ReplyDeleteNice Information, Know more on Android App Development Course
ReplyDeleteIf you are getting google search not working android issue then, it is recommended to update your google app to fix the latest issues and update your Google app. If you are facing any issue while updating the app, then contact Google for instant guidance. Visits our website or call us our toll free number +1 888 509 9555.
ReplyDeleteworld777 bet
ReplyDeleteFlats in ajmer road
best coaching classes for class 12 in gurgaon
jaipur kurti palazzo set
azure sentinel
azure databricks
azure data lake
azure expressroute
azure application gateway
ReplyDeleteif you want to get online support through chat then directly go our supportive website to fix all kind of technical query
Google Play Support
Google Play Help Center
This comment has been removed by the author.
ReplyDelete