简体   繁体   中英

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.

you can get current location coordinates by CLLocationManager as :

let sourcelocation = self.locationManager.location?.coordinate

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM