Positioning by Google Location Services
In addition to the basic platform positioning SDK for Android provides positioning by means of Google Location Services.
Google Location Services information
To use positioning by Google Location Services, Google Play Services must be available on the target devices. Please refer to developers.google.com for more information.
Using Google Location Services
To start using Google Location Services in the Android applications, perform the following steps:
- Add the dependency on the Google Location Services in your application
- Add the required Android permissions to your application
- Set the positioning data source to Google Location Services and start the Positioning Manager
- Receive and handle location updates
In detail, the following actions need to be taken:
1) Add the dependency on the Google Location Services
Add a new build rule under dependencies for the latest version of Google Location Services in build.gradle
:
dependencies {
(...)
implementation 'com.google.android.gms:play-services-location:17.0.0'
(...)
}
2) Add the Android permissions to your application
Update AndroidManifest.xml
with the following permissions:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
3) Change the positioning data source and start PositioningManager
m_googleLocationDataSource = LocationDataSourceGoogleServices.getInstance();
if (m_googleLocationDataSource != null) {
PositioningManager pm = PositioningManager.getInstance();
pm.setDataSource(m_googleLocationDataSource);
pm.addListener(new WeakReference<PositioningManager.OnPositionChangedListener>(this));
if (pm.start(PositioningManager.LocationMethod.GPS_NETWORK)) {
// Position updates started successfully.
}
}
4) Receive and handle location updates
The location updates are handled by onPositionUpdated()
(when the device location changes) and onPositionFixChanged()
(when location method changes) methods from OnPositionChangedListener
:
@Override
public void onPositionUpdated(final PositioningManager.LocationMethod locationMethod, final GeoPosition geoPosition, final boolean mapMatched) {
// new position update received
}
@Override
public void onPositionFixChanged(PositioningManager.LocationMethod locationMethod, PositioningManager.LocationStatus locationStatus) {
// positioning method changed
}
-
PositioningManager.OnPositionChangedListener
represents an interface for the position update listeners. -
GeoPosition
carriesGeoCoordinate
that contains the device WGS84 Latitude/Longitude coordinates and altitude with double precision. In addition,GeoPosition
carries location uncertainty estimate as well as speed and heading information.