Monday 5 September 2011

Getting Latitude and Longtitude in Android

Code Snippet to get latitude and longitude,

Retrieves the latitude and longitude based on current location. The corresponding class should implement LocationListener, callback notifies user in case of any change in latitude and longitude.

Code:
 private void getLatLon()
        {
            LocationManager locationManager =
                (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Location location = locationManager
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null)
            {
                lat = location.getLatitude();
                lon = location.getLongitude();          
            }       
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30000, 100,this);
        }
        @Override
        public void onLocationChanged(Location loc)
        {
            lat = loc.getLatitude();
            lon = loc.getLongitude();  
            System.out.println("Lat and long here is"+lat +"--------->>"+lon);
        }
        @Override
        public void onProviderDisabled(String arg0) {
        }
        @Override
        public void onProviderEnabled(String arg0) {
        }
        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2)
        {
        }


Android Permission :

ACCESS_COARSE_LOCATION
ACCESS_MOCK_LOCATION
CONTROL_LOCATION_UPDATES

No comments:

Post a Comment