Getting Current location from GPS in Android

We can get the current location from Android phones using

- GPS provider
- Network Provider

For this, first create an instance of location manager.

Create a class which implements the LocationListener

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

Then in the manifest we need to give the permission as,


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

For GPS provider, we will use this statement,


locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);


and for getting information from network provider, that means , we want to get the current location when inside a building or where, GPS dont work network is there, then we can use this statement for getting location information as,


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


Then from onLocationChanged() in the locationListener, we will get the latitude and longitude using getLatitude() and getLongitude()

Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android