2017-02-28 71 views
1

好的,希望這是一個簡單的快速問題。如何將當前位置設置爲request.source Swift 3

我正在構建使用Apple Maps的mapView,並且工作正常,我的segue向地圖注入經緯度,我想從當前位置導航到包含lat和lon值的註釋。

我已經設法在地圖上顯示當前位置並顯示我的註釋,但我不確定如何將當前位置設置爲以下代碼行中的源點。

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

這使用我目前的緯度和經度從我的segue注入變量。我希望request.source行成爲我的當前位置,lat和lon vars是我已經擁有的目的地。

我只是不確定的語法,使request.source我的當前位置。

這是參考了我的整個VC代碼:

進口的UIKit 進口MapKit 進口CoreLocation

類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 

} 

回答

1

您必須覆蓋CLLocationManager.didUpdateLocations在位置管理器檢索當前位置時得到通知。您必須應用CLLocationManager委託。

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) 
    } 
相關問題