Geo location

To use the location feature, you need to add permission to the AndroidManifest.xml file. Since API 23+ app must request permission at runtime.

<manifest ... >
  <!-- To request foreground location access, declare one of these permissions. -->
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  
 <!-- Required only when requesting background location access on
       Android 10 (API level 29) and higher. -->
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />  
</manifest>

The system considers your app to be using foreground location if a feature of your app accesses the device's current location in one of the following situations:

  • an activity that belongs to your app is visible
  • your app is running a foreground service

Additionally, it's recommended that you declare a foreground service type, and since API 29+ it is necessary.

<service
    android:name="MyNavigationService"
    android:foregroundServiceType="location" ... >
    <!-- Any inner elements would go here. -->
</service>

App can access to locations via LocationManager.

val locationManager: LocationManager =
        context.getSystemService(Context.LOCATION_SERVICE) as LocationManager

There are built-in location providers:

  • LocationManager.GPS_PROVIDER - uses GNSS satellites (GPS, GLONASS, etc)
  • LocationManager.NETWORK_PROVIDER - uses nearby cell tower and WiFi access points
  • LocationManager.PASSIVE_PROVIDER - uses locations obtained by other apps

To handle locations app must request location updates. When locations are no longer needed app should remove LocationListener object.

Request location updates

mock location

Android provides an ability to set mock locations for developers. So user must activate developer mode and enable mock locations.

Also you can add own test provider with the addTestProvider() method. New provider will replace any provider with the same name that exists prior to this call.

Set mock location

satellites

The GnssStatus class represents the current state of the GNSS engine. For example you can obtain count of satellites.

You can handle NMEA sentences from GPS via OnNmeaMessageListener interface.

GNSS status example

detection area

App can specify one or more detection areas. When the device detects that it has entered or exited the area surrounding the location, the given PendingIntent will be used to create an Intent to be fired.

The fired Intent will have a boolean extra added with key LocationManager.KEY_PROXIMITY_ENTERING. If the value is true, the device is entering the proximity region; if false, it is exiting. Due to the approximate nature of position estimation, if the device passes through the given area briefly, it is possible that no Intent will be fired.

After the number of milliseconds given by the expiration parameter, the location manager will delete this proximity alert and no longer monitor it. A value of -1 indicates that there should be no expiration time.

Set proximity alert

Further app can register receiver and notify user about the proximity event.

Proximity BroadcastReceiver

Google Play Services

You can work with geo locations via

  • location API
  • Places SDK
  • Google Maps SDK

In this case Google Play Services must be installed on user device.

Request update example