Traditionally, Situm WYF and Situm SDK could be used together but under a somewhat limited fashion. Essentially, if your application started positioning using Situm SDK, you needed to stop Situm SDK manually before loading Situm WYF. Then, Situm WYF would start Situm positioning on its own. If you unloaded Situm WYF, positioning would stop and you needed to re-start it again using Situm SDK.
This situation was not ideal, so from version 0.25.0 (Android) & 0.19.0 (iOS) onwards Situm SDK included in the LocationManager (Android, iOS) a new method called addLocationListener/addDelegate (Android, iOS). This method allows you to subscribe a new LocationListener/LocationDelegate (Android, iOS) to Situm SDK positioning.
If positioning is already running and you start Situm WYF, it will draw the user position immediately and will not need to restart positioning anymore. Situm WYF will act similarly at any time if the positioning stops or starts from the outside (using the Situm SDK API directly).
You may desubscribe at any time using the removeLocationListener/removeDelegate (Android, iOS) or removeAllLocationListeners (Android only) methods, and stop positioning using the removeUpdates (Android, iOS) and removeUpdates(LocationListener) (Android only) methods.
Code example #
Let’s look at the hypothetical case of an app that starts Situm positioning as soon as it is launched. This app also integrates Situm WYF on a specific screen to show the map, user position and allow navigation:
- First, we configure Situm SDK and subscribe to the geolocations that it will produce. Then, we start positioning.
- Under a call to loadWYF(), the app displays the WYF module. Situm WYF registers (internally) its own LocationListener, receiving every location and status change.
- Therefore, Situm WYF knows that the positioning was already started and will show the user position immediately. Situm WYF will react coherently when calling stopPositioning() and startPositioning() at any time. For instance, the user location will not be shown if the app stops positioning.
public class MainActivity extends AppCompatActivity { // First, declare a LocationListener. This will receive the computed geolocations, status changes and errors. private LocationListener locationListener = new LocationListener() { ... }; // Declare the SitumMapsLibrary (WYF) SitumMapsLibrary mapsLibrary = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize SitumSDK and start positioning (see https://situm.com/docs/mobile-sdks-positioning/). SitumSdk.init(this); ... // Use the addLocationListener method to receive location updates on your LocationListener implementation SitumSdk.locationManager().addLocationListener(locationListener); // Start positioning: this.startPositioning(); } public void startPositioning() { // Build the LocationRequest, ask for permissions ... LocationRequest locationRequest = new LocationRequest.Builder().build(); .... // Start positioning, passing only the desired configuration (LocationRequest). // No need to pass the LocationListener object because it was already registered SitumSdk.locationManager().requestLocationUpdates(locationRequest); } public void stopPositioning() { // Use the method removeUpdates() (without parameters). // This will keep all the LocationListeners previously subscribed. SitumSdk.locationManager().removeUpdates(); } public void loadWYF() { // This will be called somewhere in your app (e.g. from a tab navigator) // Setups and loads WYF as explained in https://situm.com/docs/android-quickstart-guide/ LibrarySettings librarySettings = new LibrarySettings(); ... mapsLibrary.load(); } }
class ViewController: UIViewController { // View where the library will be loaded @IBOutlet var containerView: UIView! // Declare the SitumMapsLibrary (WYF) var library: SitumMapsLibrary? ... override func viewDidLoad() { super.viewDidLoad() // Setups WYF as explained in https://situm.com/docs/ios-quickstart-guide/ let settings = LibrarySettings.Builder().build() library = SitumMapsLibrary(containedBy: containerView, controlledBy: self, withSettings: settings) // Configure Situm SDK explained in https://situm.com/docs/a-basic-swift-ios-app/ SITServices.provideAPIKey("SET YOUR APIKEY HERE", forEmail: "SET YOUR EMAIL HERE") // Use the addDelegate method to receive location updates. See extension below this class SITLocationManager.sharedInstance().addDelegate(self) } func startPositioning() { // Build the SITLocationRequest, ask for permissions ... // Check the quickstart guide for SitumSDK https://situm.com/docs/a-basic-swift-ios-app/ let request = SITLocationRequest() .... // Start positioning, passing only the desired configuration (SITLocationRequest) SITLocationManager.sharedInstance().requestLocationUpdates(request) } func stopPositioning() { // Use the method removeUpdates() // This will keep all the LocationListeners previously subscribed. SITLocationManager.sharedInstance().removeUpdates() } func loadWYF() { // This will be called somewhere in your app (e.g. from a tab navigator) do { try self.library!.load() } catch { print("An error has ocurred. Your SitumView could not be loaded.") } } private func unloadWYF(){ //This will be called somewhere in your code to release the resources Wayfinding is using //This is specially important to guarantee proper working if you plan to load a new instance of Wayfinding (for instance to show a different building) library?.unload() } } // Declare an extension of SITLocationDelegate. This will receive the computed geolocations, status changes and errors. extension ViewController: SITLocationDelegate { func locationManager(_ locationManager: SITLocationInterface, didInitiatedWith request: SITLocationRequest) {} func locationManager(_ locationManager: SITLocationInterface, didUpdate location: SITLocation) {} func locationManager(_ locationManager: SITLocationInterface, didFailWithError error: Error?) {} func locationManager(_ locationManager: SITLocationInterface, didUpdate state: SITLocationState) {} }