Professional Documents
Culture Documents
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:
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
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
SELECT ALL
JAVA
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)
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
(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
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
SELECT ALL
JAVA
SELECT ALL
JAVA
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
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
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
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
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/)
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