Skip to main content

Display user location

The application owns location permissions and update policy. GLMap only displays the locations it receives.

Swift

GLMapUserLocation implements CLLocationManagerDelegate and draws the position, movement direction, and accuracy. Add user_location.svg and user_movement.svg from the reference app to your application target, then connect the helper directly to Core Location:

import CoreLocation
import GLMap

private let locationManager = CLLocationManager()
private var userLocation: GLMapUserLocation?

override func viewDidLoad() {
super.viewDidLoad()

guard let userLocation = GLMapUserLocation(drawOrder: 100) else {
fatalError("user_location.svg or user_movement.svg is missing")
}
self.userLocation = userLocation
userLocation.add(toMap: map)
locationManager.delegate = userLocation
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}

deinit {
locationManager.stopUpdatingLocation()
}

Add NSLocationWhenInUseUsageDescription to Info.plist. Keep both the manager and GLMapUserLocation alive for as long as updates are required. The Swift demo also shows how to forward callbacks when the map controller needs to animate each update itself.

Kotlin

Android applications normally obtain locations from FusedLocationProviderClient. The GLMap-specific update is small: convert the coordinate, update the marker, and scale an optional accuracy circle in internal map units.

val position = MapPoint.CreateFromGeoCoordinates(
location.latitude,
location.longitude,
)
val accuracyScale = renderer.convertMetersToInternal(
location.accuracy.toDouble(),
) / 2048.0

renderer.animate { animation ->
animation.setTransition(GLMapAnimation.Linear)
animation.setDuration(1.0)
animation.setPosition(locationImage, position)
animation.setPosition(accuracyCircle, position)
animation.setScale(accuracyCircle, accuracyScale)
}

Request ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION through the Android activity-result API. Stop updates and call map.dispose() when the screen is destroyed.

The complete compact setup, including permission handling, SVG marker creation, accuracy geometry, and cleanup, is in the reference apps: