You are on page 1of 10

12/4/2017 How to get and use location data in your Android app | AndroidAuthority

How to get and use location data in your


Android app
ANDROID DEVELOPMENT (HTTPS://WWW.ANDROIDAUTHORITY.COM/ANDROID-DEVELOPMENT/) NEWS (HTTPS://WWW.ANDROIDAUTHORITY.COM/NEWS/)

412   
or/obaroogbo/)
by Obaro Ogbo July 15, 2015 (https://plus.google.com/share?
(https://twitter.com/intent/tweet?
(https://www.facebook.com/sharer.php?
url=https://www.androidauthority.com/get-
url=https://www.androidauthority.com/get-
app_id=598473026880919&u=https://www.androidauthority.com/get-
use- use- use-
location- location- location-
data- data- data-
android- android- android-
app- app- app-
625012/)625012/&text=How+to+get+and+use+location+data+in+your+Android+app)
625012/&t=How+to+get+and+use+location+data+in+your+Android+app)

Shutterstock (http://www.shutterstock.com/)

Using Location in your app has incredible potential in making your app seem intelligent to end users. With location data, your app can
predict a user’s potential actions, recommend actions, or perform actions in the background without user interaction.

For this article, we shall discuss integrating location updates into an Android app, with a focus on fetching the latitude and longitude of a
given Location only. It is worth pointing out that Location (https://developer.android.com/reference/android/location/Location.html) can
(and does) contain much more than just latitude and longitude values. It can also have values for the bearing, altitude and velocity of the
device.

Preparation
Before your app can receive any location data, you must request location permissions. There are two location permissions,
ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION. We use ACCESS_FINE_LOCATION to indicate that we want to receive as precise a
location as possible. To request this permission, add the following to your app manifest:

SELECT ALL
XML

SELECT ALL
XML

https://www.androidauthority.com/get-use-location-data-android-app-625012/ 1/10
12/4/2017 How to get and use location data in your Android app | AndroidAuthority

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.foo.simplelocationapp" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</maniifest>

We also need to select which of the location providers we want to use to get the location data. There are currently three providers:

. GPS_PROVIDER: The most accurate method, which uses the built-in GPS receiver of the device. GPS is a system of satellites in orbit,
that provides location information from almost anywhere on earth. It can sometimes take a while to get a GPS location fix (generally
faster when the device is outdoors).
. NETWORK_PROVIDER: This method determines a device’s location using data collected from the surrounding cell towers and WiFi
access points. While it is not as accurate as the GPS method, it provides a quite adequate representation of the device’s location.
. PASSIVE_PROVIDER: This provider is special, in that it indicates that your app doesn’t want to actually initiate a location fix, but uses
the location updates received by other applications/services. In other words, the passive provider will use location data provided by
either the GPS or NETWORK providers. You can find out what provider your passive provider actually used with the returned
Location’s getProvider() method. This provides the greatest battery savings.

Layout

(https://cdn57.androidauthority.net/wp-content/uploads/2015/07/aa_location_layout.jpg)

For our app, we are going to fetch location data using the GPS provider, the NETWORK provider, and also by asking the device to decide
which is the best available provider that meets a given set of criteria. Our layout has three identical segments, each of which contains:

. A title for the section, such as GPS LOCATION


. A Button to resume and pause location updates for the section/provider
. Longitude Value
. Latitude Value
The code snippet for the GPS section, from our layout/activity_main.xml file is shown below

SELECT ALL
XML

SELECT ALL
XML

https://www.androidauthority.com/get-use-location-data-android-app-625012/ 2/10
12/4/2017 How to get and use location data in your Android app | AndroidAuthority

<TextView
android:id="@+id/titleTextGPS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="GPS LOCATION"
android:textSize="20sp"/>

<Button
android:id="@+id/locationControllerGPS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/titleTextGPS"
android:text="@string/resume"
android:onClick="toggleGPSUpdates"/>

<TextView
android:id="@+id/longitudeTextGPS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/locationControllerGPS"
android:text="longitude"
android:textSize="20sp"/>

<TextView
android:id="@+id/longitudeValueGPS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/locationControllerGPS"
android:layout_toRightOf="@id/longitudeTextGPS"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:text="0.0000"
android:textSize="20sp"/>

<TextView
android:id="@+id/latitudeTextGPS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/longitudeTextGPS"
android:text="latitude"
android:textSize="20sp"/>

<TextView
android:id="@+id/latitudeValueGPS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/longitudeValueGPS"
android:layout_toRightOf="@id/longitudeTextGPS"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:text="0.0000"
android:textSize="20sp"/>

MainActivity
It is possible that the user has their device Location settings turned off. Before requesting location information, we should check that
Location services are enabled. Polling for location data with the settings turned off will return null. To check if Location is enabled, we
implement a method, called isLocationEnabled(), shown below:

SELECT ALL
JAVA

SELECT ALL
JAVA

private boolean isLocationEnabled() {


return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

We simply ask the LocationManager if either the GPS_PROVIDER or the NETWORK_PROVIDER is available. In the case where the user has
Location turned off, we want to help them get to the Location screen as easily and quickly as possible to turn it on and get back into our app.
To do this, we implement the showAlert() method.

https://www.androidauthority.com/get-use-location-data-android-app-625012/ 3/10
12/4/2017 How to get and use location data in your Android app | AndroidAuthority

JAVA SELECT ALL

SELECT ALL
JAVA

private void showAlert() {


final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enable Location")
.setMessage("Your Locations Settings is set to 'Off'.\nPlease Enable Location to " +
"use this app")
.setPositiveButton("Location Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
}
});
dialog.show();
}

The most interesting line in the snippet above is within the setPositiveButton() method. We start an activity using the
Settings.ACTION_LOCATION_SOURCE_SETTINGS intent, so that when the user clicks on the button, they are taken to the Location Settings
screen.

(https://cdn57.androidauthority.net/wp-content/uploads/2015/07/aa_location_settings.jpg)

Getting location updates


To get GPS and Network location updates, we use one of the LocationManager’s requestLocationUpdates() methods. Our preferred is
requestLocationUpdates(String provider, int updateTime, int updateDistance, LocationListener listener). updateTime refers to the frequency
with which we require updates, while updateDistance refers to the distance covered before we require an update. Note that updateTime
simply specifies the minimum time period before we require a new update. This means that the actual time between two updates can be
more than updateTime, but won’t be less.
A very important point to consider is that Location polling uses more battery power. If your app doesn’t require location updates when in
the background, consider stopping updates using one of the removeUpdates() methods. In the code snippet below, we stop/start location
updates in response to clicking on the relevant Button.

SELECT ALL
CPP

SELECT ALL
CPP

https://www.androidauthority.com/get-use-location-data-android-app-625012/ 4/10
12/4/2017 How to get and use location data in your Android app | AndroidAuthority

public void toggleGPSUpdates(View view) {


if(!checkLocation())
return;
Button button = (Button) view;
if(button.getText().equals(getResources().getString(R.string.pause))) {
locationManager.removeUpdates(locationListenerGPS);
button.setText(R.string.resume);
}
else {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 2 * 60 * 1000, 10, locationListenerGPS);
button.setText(R.string.pause);
}
}

(https://cdn57.androidauthority.net/wp-content/uploads/2015/07/aa_location_gps.jpg)

For both NETWORK_PROVIDER and PASSIVE_PROVIDER, simply replace GPS_PROVIDER above with your desired provider.

In the case where you just want to pick the best available provider, there is a LocationManager method, getBestProvider() that allows you do
exactly that. You specify some Criteria to be used in selecting which provider is best, and the LocationManager provides you with whichever
it determines is the best fit. Here is a sample code, and it’s what we use to select a provider:

SELECT ALL
CS

SELECT ALL
CS

Criteria criteria = new Criteria();


criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
if(provider != null) {
locationManager.requestLocationUpdates(provider, 2 * 60 * 1000, 10, locationListenerBest);
button.setText(R.string.pause);
Toast.makeText(this, "Best Provider is " + provider, Toast.LENGTH_LONG).show();
}

Using the above code and Criteria, the best provider will be GPS_PROVIDER, where both GPS and NETWORK are available. However if the
GPS is turned off, the NETWORK_PROVIDER will be chosen and returned as the best provider.

https://www.androidauthority.com/get-use-location-data-android-app-625012/ 5/10
12/4/2017 How to get and use location data in your Android app | AndroidAuthority

(https://cdn57.androidauthority.net/wp-content/uploads/2015/07/aa_location_best_provider.jpg)

LocationListener
The LocationListener is an interface for receiving Location updates from the LocationManager. It has four methods

. onLocationChanged() – called whenever there is an update from the LocationManager.


. onStatusChanged() – called when the provider status changes, for example it becomes available after a period of inactivity
. onProviderDisabled() – called when the user disables the provider. You might want to alert the user in this case that your app
functionality will be reduced
. onProviderEnabled() – called when the user enables the provider
We implement only the onLocationChanged() method for this sample, but in a production app, you will most likely want to perform an
appropriate action for each situation.

SELECT ALL
JAVA

SELECT ALL
JAVA

private final LocationListener locationListenerNetwork = new LocationListener() {


public void onLocationChanged(Location location) {
longitudeNetwork = location.getLongitude();
latitudeNetwork = location.getLatitude();
runOnUiThread(new Runnable() {
@Override
public void run() {
longitudeValueNetwork.setText(longitudeNetwork + "");
latitudeValueNetwork.setText(latitudeNetwork + "");
Toast.makeText(MainActivity.this, "Network Provider update", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};

Android Developer Newsletter


Do you want to know more? Subscribe to our Android Developer Newsletter (http://devweekly.androidauthority.com). Just type in your
email address below to get all the top developer news, tips & links once a week in your inbox:
Email:

Subscribe

PS. No spam, ever. Your email address will only ever be used for Android Dev Weekly.

Conclusion
The complete source code is available on github (https://github.com/obaro/SimpleLocationApp), for use (or misuse) as you see fit. Just be
mindful of the following:
https://www.androidauthority.com/get-use-location-data-android-app-625012/ 6/10
12/4/2017 How to get and use location data in your Android app | AndroidAuthority

. Integrating location tracking in your apps drains the battery.


. Google recommends apps request updates every 5 minutes (5 * 60 * 1000 milliseconds). You might need faster updates if your app is
in the foreground (fitness/distance tracker).
. Users can have location turned off. You should have a plan for when location settings is turned off.
Have fun building Android apps, and watch out for our upcoming tutorial where we make use of Android device location data and web APIs
to build something even more fun and challenging.

ANDROID DEVELOPMENT (HTTPS://WWW.ANDROIDAUTHORITY.COM/ANDROID-DEVELOPMENT/) NEWS (HTTPS://WWW.ANDROIDAUTHORITY.COM/NEWS/)

 GPS (https://www.androidauthority.com/tag/gps/), Location Aware (https://www.androidauthority.com/tag/location-aware/)


GPS (https://www.androidauthority.com/tag/gps/), Location Aware (https://www.androidauthority.com/tag/location-aware/)

Obaro Ogbo (https://www.androidauthority.com/author/obaroogbo/)

Popular In the Community

Sponsored OBSERVATIONS ON SNAPCHAT IS BURNING USERS WITH THE PLY IS A MOTORCYCLE LOWEST PRICE EVER
CHROME OS, ANDROID… ITS APP TO THE GROUN… NOVEMBER SECURITY… SMART HELMET THAT… UNLIMITED (UPDAT

nebulao OrangeGoggles GreenFan OliveDog Ron Ablang


22h 4d 2d 13h 16h

AA you do everything it doesnt matter what I have found rolling Can't help but think it What a deal.
possible to piss o t… snapchat does! But… back to the stock… might be wiser to…

https://www.androidauthority.com/get-use-location-data-android-app-625012/ 7/10
12/4/2017 How to get and use location data in your Android app | AndroidAuthority

Google amends its Google Play “Best of


2017” list — but why?
68   
://www.androidauthority.com/author/scottadamgordon/)
NEWS (HTTPS://WWW.ANDROIDAUTHORITY.COM/NEWS/) by Scott Adam Gordon 52 minutes ago (https://plus.google.com/share?
(https://twitter.com/intent/tweet?
(https://www.facebook.com/sharer.php?
url=https://www.androidauthority.com/google-
url=https://www.androidauthority.com/google-
app_id=598473026880919&u=https://www.and
play- play- play-
best- best- best-
2017- 2017- 2017-
list- list- list-
change- change- change-
819822/)819822/&text=Google+amends+its+Google+Play+%26
819822/&t=Google+amends+its+Google+Play

We covered the Google Play Best of 2017 (https://www.androidauthority.com/google-plays-best-apps-games-books-movies-tv-shows-2017-


819347/) awards last week, in which Google outlined the top apps, games, book, movies and TV shows that the store had seen this year.
Over the weekend, Google quietly made some amendments to that original list and it has left us scratching our heads as to why.

The changes were spotted by Spanish-language website Android Jefe and among them was a removal of the “most popular apps” and “most
popular games” sections (check out the original post here
(https://web.archive.org/web/20171201230958/https://blog.google/products/google-play/announcing-google-plays-best-2017/)). These have
now been replaced by the “best app” and “best game” categories. It’s an odd adjustment, given that Google had only just published the
article and those were the first two sections (you would expect that the company was pretty confident about including them), but it’s not
unthinkable.

After some digging, it seems that changes didn’t just arrive to Google’s original blog post, but its most popular apps list too. The app
previously in the number one slot of this section — Photo Editor – Beauty Camera & Photo Filters
(https://play.google.com/store/apps/details?id=com.pic.photoeditor&hl=en) — has now been swapped for another photo-centric app
called FaceApp
(https://webproxy.to/browse.php/lHhleO4b/gAGZWWXc/Ij6EJu3Y/mvSEIgzB/kcNkRe4n/8lgbQTO8/3K1mdwW8/K4tc6D3f/3MSdUA_3/D_3D/b5/

Photo Editor – Beauty Camera & Photo Filters has been installed between 10 and 50 EDITOR'S PICK
million times and has an average rating of 4.2 stars. It’s undoubtedly popular, but it
(https://www.androidauthority.com/google
requests a whole bunch of uncommon camera app permissions, some of which might
home-deal-best-buy-819465/)
set alarm bells ringing. These include: rerouting outgoing calls, making calls directly,
Deal: Get a Google Home for just
reading and writing the call log, and changing/intercepting network settings and traffic. $79.99 at Best Buy (Update:
Its use of lock screen ads is also at odds with current Google Policy Update (12/4/17): Along with the Google Home

(https://play.google.com/about/monetization-ads/ads/lockscreen/), and Google said a


few days ago that it would be cracking down on apps that collect and transmit personal
data “unrelated to the functionality of the app.” It isn’t clear if these factors are connected to this situation, however.

The most popular apps section was likely based on raw numbers rather than the opinion of Google employees, so it’s possible that Google
has simply overlooked the app when it first published the list. While this would be disappointing, especially considering that many websites
that picked up the list may not have noticed the subsequent amendment, what’s worse is that we don’t know why it’s gone. Was it just
included in error? Has Google since judged it to be low-quality and not fit for its best list? Is there a deeper problem with the app?

I’ve reached out to Google regarding the matter and will update this article should I receive a response. What’s your take on Google’s “best
of” list? Let us know in the comments.

https://www.androidauthority.com/get-use-location-data-android-app-625012/ 8/10
12/4/2017 How to get and use location data in your Android app | AndroidAuthority

NEWS (HTTPS://WWW.ANDROIDAUTHORITY.COM/NEWS/)

 Google (https://www.androidauthority.com/tag/google/), Google Play Store (https://www.androidauthority.com/tag/google-play-store/)


Google (https://www.androidauthority.com/tag/google/), Google Play Store (https://www.androidauthority.com/tag/google-play-store/)

Scott Adam Gordon (https://www.androidauthority.com/author/scottadamgordon/)


Scott Adam Gordon is a European correspondent for Android Authority. Follow him on Twitter
(https://twitter.com/ScottAdamGordon) and Google+

https://www.androidauthority.com/get-use-location-data-android-app-625012/ 9/10
12/4/2017 How to get and use location data in your Android app | AndroidAuthority

https://www.androidauthority.com/get-use-location-data-android-app-625012/ 10/10

You might also like