Skip to main content
Version: 1.x

Writing a Custom Location Source

Drive the Wemap map, AR view and navigation from a location provider of your own.

Overview​

Use a location provider of your own, such as a BLE, UWB or Wi-Fi indoor positioning vendor, by conforming a type to LocationSource.

You implement two methods, and the SDK calls them: startUpdating(delegate:) and stopUpdating(). The SDK starts and stops your source as the map and AR views need it. Each time it starts the source, it hands it a LocationSourceDelegate: report every update from your provider to that delegate until the source is stopped.

What you report is processed exactly like the updates of Wemap's own sources. Each coordinate is projected on the navigation's itinerary, or on the map's graph, when projection is enabled, and then published on the session's coordinates stream, which the map's user location, the AR view and navigation all read.

Implementing a source​

final class BeaconLocationSource: LocationSource {

let coreSession: CoreSession
private let provider: BeaconProvider

init(session: CoreSession, provider: BeaconProvider) {
coreSession = session
self.provider = provider
}

func startUpdating(delegate: LocationSourceDelegate) {
provider.startUpdates { @Sendable fix in
let coordinate = Coordinate(
latitude: fix.latitude, longitude: fix.longitude, level: fix.floor
)
delegate.didUpdate(coordinate: coordinate)
} onFailure: { @Sendable error in
delegate.didFail(with: error)
}
}

func stopUpdating() {
provider.stopUpdates()
}
}
  • coreSession is the session the source reports to. Create the source with the same session as the map or AR view it drives.
  • startUpdating(delegate:) is called by the SDK. The delegate can be called from any thread. Every start gets a new delegate, and the previous one stops forwarding: an update your provider delivers after the source was stopped, replaced or deallocated is dropped.
  • Mark a provider callback @Sendable, as above. Without it, a closure written inside the source inherits its main-actor isolation, and the process traps when the provider calls it from another thread.
  • stopUpdating() is called by the SDK when the source stops: stop your provider's updates there.
  • isStarted is kept by the SDK. Implement it only if startUpdating can refuse to start.
  • supportsAttitude is false by default, and the SDK then reads the device's attitude from its own sensors. Return true only if your provider reports the attitude, through didUpdate(attitude:).
  • didFail(with:) publishes the error on the session's errors stream. Report each failure once, when it happens: an error is an event, not a condition repeated while it lasts. When your provider loses the user, report trackingLost; the next coordinate clears it.

There is nothing to release: when the last reference to the source goes, the map and AR views stop using it, and its delegate stops forwarding. Free your provider's resources in the source's deinit.

Attaching it​

Assign the source to the map or to the AR view, as any Wemap location source:

let source = BeaconLocationSource(session: session, provider: provider)

mapView.userLocationManager.locationSource = source
// or
arView.locationManager.locationSource = source

To use the source without a Wemap view, start it yourself with start(), consume its coordinates, attitudes and errors, and stop it with stop(). A source you start yourself stays yours to stop.

Never call startUpdating or stopUpdating yourself: start() and stop() are what keep the SDK's view of the source right. For the same reason, don't declare a start() or stop() of your own: called on your concrete type, it would hide the SDK's.