Thursday, June 16, 2011

Get Current Location in Android / Obtaining User Location

In Android Three Approaches are preferred to Get Current Location -> Using GPS -> Cell - Id -> Wi-Fi The above three gives a clue to get the current location. Gps gives the results accurately. But it consumes Battery. Android Network Location Provider get the current location based wifi signals and cell tower. We have to use both  or a single to Get the Current Location
LocationManager myManager;
MyLocListener loc;
loc = new MyLocListener();
myManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc );
package com.gps.activities;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
public class MyLocListener implements LocationListener
{
    //Here you will get the latitude and longitude of a current location.
    @Override
    public void onLocationChanged(Location location)
    {
            if(location != null)
            {
                     Log.e("Latitude :", ""+location.getLatitude());
                     Log.e("Latitude :", ""+location.getLongitude());
            }
    }
    @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
       
    }
}
Note: 
 Include the below permissions in manifast.xml to listen Location Updates.

<uses-permission       android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission     android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Note : If you want more details about Obtaining User Location/ Get User Location refer the below link. http://developer.android.com/guide/topics/location/obtaining-user-location.html

No comments:

Post a Comment