Jade Reynolds Jade Reynolds -4 years ago 161
Swift Question

How to set current location into request.source Swift 3

ok, Hopefully an easy quick question.

I am building a mapView that uses Apple Maps and works fine, my segue injects a latitude and longitude into the map, and i want to navigate from current location to the annotation that contains the lat and lon values.

I have managed to get current location showing on the map and my annotation showing, but I'm not sure how to set my current location as the source point in the following line of code.

request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat,longitude: lon)))


this uses my current vars of lat and lon that are injected from my segue. I want the request.source line to be my current location, the lat and lon vars are for the destination which I already have.

I'm just not sure on the syntax to make request.source my current location.

This is my whole VC code for reference:

import UIKit
import MapKit
import CoreLocation

class LocateVC: UIViewController, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!
var lat: Double!
var lon: Double!
var name: String!
var locationManager = CLLocationManager()

func checkLocationAuthorizationStatus() {
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
mapView.showsUserLocation = true
} else {
locationManager.requestWhenInUseAuthorization()
}
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
checkLocationAuthorizationStatus()
}

override func viewDidLoad() {
super.viewDidLoad()

annotation.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
mapView.addAnnotation(annotation)


let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat,longitude: lon)))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat,longitude: lon)))
request.requestsAlternateRoutes = true
request.transportType = .automobile

let directions = MKDirections(request: request)

directions.calculate {[unowned self] response, error in
guard let unwrappedResponse = response else { return }

for route in unwrappedResponse.routes {
self.mapView.add(route.polyline)
self.mapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
}
}
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.blue
return renderer
}

let regionRadius: CLLocationDistance = 1000
let annotation = MKPointAnnotation()
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
mapView.showsUserLocation = true

}

Answer Source

You have to override CLLocationManager.didUpdateLocations to get notified when the location manager retrieves the current location. You have to apply CLLocationManager delegate.

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        let location = locations.last as CLLocation

        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)        
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

        self.map.setRegion(region, animated: true)
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download