Reputation: 25
I'm trying to find a way to implement the "current location" on Xcode using MapKit.
What would be the steps to bring this out if i already have the map?
Thanks!
Upvotes: 1
Views: 47
Reputation: 4980
To gain the current location, we need to harness CLLocation
, so import the framework into your application, which will provide you with the ability to fetch the users location. Next, you will need to add a PLIST entry to alert the user asking them if they would like your app to utilize location services. You do this by either adding NSLocationAlwaysUsageDescription
, orNSLocationWhenInUseUsageDescription
, and setting a string value to it. This string value will be what the user reads on the prompt. Then, in the function of your choosing, you will use code similar to this (POC code, edit as needed):
class MyClass: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
// Making the class conform to the CLLocationManagerDelegate and the MKMapViewDelegate
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestAlwaysAuthorization()
mapLocations.showsUserLocation = true
let mapCenter = mapLocations.userLocation.coordinate
self.mapLocations.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true);
}
In this code we first request authorization from the user. If they select yes, we show the user location.
Upvotes: 1