ok、うまくいけば簡単に簡単に質問できます。現在の場所をrequest.sourceに設定する方法Swift 3
アップルマップを使用してうまく動作するmapViewを構築しています。私のsegueは緯度と経度をマップに挿入し、現在の位置から緯度と経度の値を含む注釈にナビゲートします。
地図上に現在地が表示されていて、アノテーションが表示されていましたが、次のコード行で現在の場所をソースポイントとして設定する方法がわかりません。
request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat,longitude: lon)))
これは私のsegueから注入されたlatとlonの現在の変数を使用しています。私はrequest.source行を私の現在の場所にしたい、latとlonは私がすでに持っている行き先のためのものです。
私は現在の位置を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
}