Learn how to get your current location using GPS in android

In this article we will learn how to get user location in android their are two ways to accomplish this either by using GPS Provider or by Android’s Network Location Provider.We will be using GPS to get location.

First Let us know where we can use it
Knowing where the user is allows your application to be smarter and deliver better information to the user.
When developing a location-aware application for Android, you can utilize GPS and Android’s Network Location Provider to acquire the user location.

Lets get started!!
Create a New Android Application Project
Create a new project in Eclipse by navigating to File ⇒ New ⇒ Android Project and fill all the required details.

Add permissions to AndroidManifest.xml
To run our GPS Location Manager application, we need to provide the permissions given below.
1. ACCESS_FINE_LOCATION: This permission will give the application access to the GPS location coordinates.
2. INTERNET: This permission will allow the application to use the Internet. Add the lines of code below to your Android manifest file.

Put the below mentioned code to your AndroidManifest.xml
<uses-permission android:name=”android.permission.INTERNET”⁄>
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”⁄>

Using the Android Location Manager
We be getting your current location in Android using GPS. We will use the predefined LocationListener to obtain location information from the device.
In your MainActivity Implement a LocationListener and make a global object for the LocationManager and implement all the unimplemented methods.
public class MainActivity extends Activity implements LocationListener {
private LocationManager locationManager;

Next we check whether GPS is enabled or no, if not then we will prompt user to enable GPS
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!isGPSEnabled)
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

alertDialog.setTitle(“GPS settings”);
alertDialog.setMessage(“GPS is not enabled. Do you want to go to settings menu?”);
alertDialog.setPositiveButton(“Settings”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});

alertDialog.setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

alertDialog.show();
}

To get the current location we’ll be using the following
if (locationManager != null) {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(getApplication(),"latitude: "+ latitude +" longitude: "+longitude, Toast.LENGTH_LONG).show();
}

Have a look at the final code
package com.example.getlocationactivity;
public class MainActivity extends Activity implements LocationListener {
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Button bt=(Button) findViewById(R.id.btn1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!isGPSEnabled)
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle(“GPS settings”);
alertDialog.setMessage(“GPS is not enabled. Do you want to go to settings menu?”);
alertDialog.setPositiveButton(“Settings”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});

alertDialog.setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

alertDialog.show();
}
else
{
if (locationManager != null) {
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(getApplication(),”latitude: “+ latitude +” longitude: “+longitude, Toast.LENGTH_LONG).show();
}
}
}
}
});
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}

Our App
null
null

You can download the whole project here https://github.com/PracticalCoding/GetLocationActivity

Hope this article was helpful……..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s