Show User's Location aka. Blue Dot
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.
class MyPositionProvider : NSObject, MPPositionProvider {Add some member variables to MyPositionProvider.
delegate: The delegate objectrunning: A running state boolean flaglatestPositionResult: The latest positioning resultpreferAlwaysLocationPermission: 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
var delegate: MPPositionProviderDelegate?
private var running = false
var latestPositionResult: MPPositionResult?
var preferAlwaysLocationPermission: Bool = false
var locationServicesActive: Bool = false
var providerType: MPPositionProviderType = .GPS_POSITION_PROVIDER
var heading:Double = 0Create 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
MPPositionResulttolatestPositionResultAssign a new position point
Optionally specify that heading is available and set a heading
Notify the delegate by calling
onPositionUpdatepassing the new position as argumentSchedule a new delayed call of this method
Implement the requestLocationPermissions method. In this example we will just set the locationServicesActive to true.
Implement the updateLocationPermissionStatus method. In this example we will just set the locationServicesActive to true.
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.
Implement the startPositioningAfter method. This is just a convenience method that should support a delayed start.
Implement the isRunning method. Return the value of running.
See the sample in MyPositionProvider.swift
Create a view controller displaying a map that shows the user's "mocked" location
Create a class ShowMyLocationController that inherits from UIViewController.
Add a GMSMapView and a MPMapControl to the class
Inside viewDidLoad, setup the map so that it shows the demo venue and initialise mapControl
Inside viewDidLoad, optionally add a special icon for the user location
Inside viewDidLoad, finally
Tell mapControl to show the users location
Assign your position provider
MyPositionProvidertoMapsIndoors.positionProviderStart positioning
Last updated
Was this helpful?