Skip to content

Location

To enable location based features, you need to call SygicEngine.openGpsConnection() method after you initialize the SDK and you need to have location permission. On this page you can find information on how to work with the location.

Requesting permissions

For location features to work, you need to have ACCESS_FINE_LOCATION (ACCESS_COARSE_LOCATION is recommended as well) permission defined in your AndroidManifest.xml. Starting from Android 10 you also need ACCESS_BACKGROUND_LOCATION permission if you want to access location information in the background.

You can read more about the permissions on Android 10 and higher on the official Android Developer site.

AndroidManifest.xml
<manifest ... >
  <!-- You should always include this permission -->
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

As this is a protected permission, you need to request it at runtime. Documentation on how to request location permission can be found at official Android documentation page.

Opening GPS connection

Info

The method SygicEngine.openGpsConnection() can be called only after you have succesfully initialized the SDK.

After the location permission is succesfully granted, you can just call the function SygicEngine.openGpsConnection() directly. An example on how to open GPS connection with requesting permission follows.

val locationPermissionRequest = registerForActivityResult(
    ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
  when {
    permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> {
      SygicEngine.openGpsConnection()
    }
  }
}

// ...

locationPermissionRequest.launch(
  arrayOf(
    Manifest.permission.ACCESS_FINE_LOCATION,
    Manifest.permission.ACCESS_COARSE_LOCATION
  )
)

After you open the GPS connection, the location indicator should display your location on the map as shown in the picture.

Location Indicator

Position Manager

There are several ways how to work with your Position in Sygic Maps SDK. You can leave everything to the SDK, or you can use the CustomPositionUpdater and pass it your own values. Let's get straight into it. To to get and see your position, you simply need to start the location updates using the PositionManager:

val mPositionManager = PositionManagerProvider.getInstance().get()
mPositionManager.startPositionUpdating()

Attention

Always get the managers after initializing the engine or use asynchronous getInstance method which will wait for the engine initialization.
The SDK chooses between the Google Play services location APIs ( uses the FusedLocationProviderClient and PRIORITY_HIGH_ACCURACY) or the Android framework location APIs depending on whether you have Google Play Services enabled.

Info

You need to request the location permission from the user, else the location won't work.

Custom position updater

Otherwise, you can use the CustomPositionUpdater to update your position in Sygic Maps SDK. You can pass any coordinates that you read from a file beforehand, get from a server or get from the Android Location module etc. Let's see how to integrate the Android Location module updates:

val mPositionManager = PositionManagerProvider.getInstance().get()
val customPositionUpdater = CustomPositionUpdater()
mPositionManager.setCustomPositionUpdater(customPositionUpdater)
val locationListener = LocationListener { location ->
    val geoCoordinates = GeoCoordinates(location.latitude, location.longitude)
    val geoPosition = GeoPosition(geoCoordinates, location.speed.toDouble(), location.bearing, location.time/1000)
    customPositionUpdater.updatePosition(geoPosition)
}
val locationManager = requireContext().getSystemService(LOCATION_SERVICE) as LocationManager
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0.1F, locationListener)