Show User's Location aka. Blue Dot
For a full implementation of this, please see here.
In this tutorial we will show how you can show a blue dot on the map, representing the users location. The position will be served from a mocked positioning provider and displayed on a map in a view controller.
We will start by creating our implementation of a positioning provider.
Create a class MyPositionProvider
that inherits from NSObject and implements MPPositionProvider
.
Add some member variables to MyPositionProvider
.
delegate
: The delegate objectrunning
: A running state boolean flaglatestPosition
: The latest positioning result
INFO
The following properties are not included in this example since position is mocked but may be necessary depending upon your use case.
preferAlwaysLocationPermission
: A boolean that indicates whether this provider requires Apple Location Services to always be activelocationServicesActive
: A boolean that indicates whether Apple Location Services is currently activeproviderType
: A provider type enum, convenient when working with multiple positioning providers in the same application
Create a method called updatePosition
. This will be our "loop" constantly posting a new position to the delegate.
Check if the provider has a running state
Assign a new
MPPositionResult
tolatestPosition
Assign a new position point
Optionally specify that heading is available and set a heading
Notify the delegate by calling
onPositionUpdate
passing the new position as argumentSchedule a new delayed call of this method
Implement the startPositioning
method. We set the running
boolean to true and call updatePos
.
Implement the stopPositioning
method. We set the running
boolean to false.
Create a view controller displaying a map that shows the user's "mocked" location
Create a class ShowMyLocationController
that inherits from UIViewController
.
Inside viewDidLoad
or any method of your app's lifecycle, generate and apply a random position, you may optionally also override the default icon for blue dot
Inside viewDidLoad
, we
Tell mapControl to show the users location
Assign your position provider
MyPositionProvider
toMapsIndoors.positionProvider
and then finally,Start positioning
It is possible to change the default icon for the user position to include heading by using an image included in the MapsIndoors bundle:
Last updated