Jade Reynolds
Jade Reynolds

Reputation: 301

current location in mapkit

I have a mapkit view that data is being sent into from a previous view, this works fine. What I need to understand is how to set my source location to the current location of the user. This is my lines of code code:

let sourcelocation = CLLocationCoordinate2D(latitude: track.lat, longitude: track.lon)
let destinationLocation = CLLocationCoordinate2D(latitude: track.lat, longitude: track.lon)

When I run, obviously this puts 2 pins in exactly the same location.

I'm not sure what to put in to the sourceLocation line to display current location.

I have the following lines in my code already:

var myLocation:CLLocationCoordinate2D

self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
}

and I have declared my delegate with:

CLLocationManagerDelegate

Im just not sure where to go now.

Ultimately I am trying to get this to plot a route and allow you to navigate from your current location to the destination location.

Upvotes: 2

Views: 5847

Answers (2)

ff10
ff10

Reputation: 3147

Since you're using MapKit, you probably have a MapView implemented at some point. If it makes sense, you can use

mapView.userLocation.location

to get the location of the device.

Upvotes: 3

Jagdeep Singh
Jagdeep Singh

Reputation: 2626

you can get current location coordinates by CLLocationManager as :

let sourcelocation = self.locationManager.location?.coordinate

Upvotes: 3

Related Questions