東京の座標を基準に、ロングタップした位置までのルートをマップに表示させたいです。
リンクを参考にしましたが、ラインを表示させられません。
html
1 2import UIKit 3import MapKit 4import CoreLocation 5 6class ViewController: UIViewController,CLLocationManagerDelegate,UIGestureRecognizerDelegate { 7 8 9 10 @IBOutlet weak var mapView: MKMapView! 11 12 var routePolyLine: MKPolyline! 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 // Do any additional setup after loading the view. 17 } 18 19 @IBAction func longTapAction(_ sender: UILongPressGestureRecognizer) { 20 21 if sender.state == .began{ 22 23 }else if sender.state == .ended{ 24 25 let tapPoint = sender.location(in: view) 26 27 let tapCenter = mapView.convert(tapPoint, toCoordinateFrom: mapView) 28 29 routeSearch(tapCenter: tapCenter) 30 } 31 } 32 33 34 public func routeSearch(tapCenter:CLLocationCoordinate2D){ 35 36 let latitude = 35.681236 37 let longitude = 139.767125 38 39 let tokyoLocation = CLLocationCoordinate2DMake(latitude, longitude) 40 41 42 let sourcePlaceMark = MKPlacemark(coordinate: tokyoLocation) 43 44 let destinationPlaceMark = MKPlacemark(coordinate: tapCenter) 45 46 let directionRequest = MKDirections.Request() 47 directionRequest.source = MKMapItem(placemark: sourcePlaceMark) 48 directionRequest.destination = MKMapItem(placemark: destinationPlaceMark) 49 50 directionRequest.transportType = .walking 51 52 let directions = MKDirections(request: directionRequest) 53 directions.calculate { (response, error) in 54 guard let directionResonse = response else{ 55 56 if let error = error{ 57 58 print("we have errpr getting directions==(error.localizedDescription)") 59 60 } 61 return 62 } 63 64 DispatchQueue.main.async { 65 66 let route = directionResonse.routes[0] 67 self.routePolyLine = route.polyline 68 self.routePolyLine.subtitle = "route" 69 self.mapView.addOverlay(self.routePolyLine) 70 71 let rect = route.polyline.boundingMapRect 72 self.mapView.setRegion(MKCoordinateRegion(rect), animated: true) 73 74 } 75 76 77 } 78 79 } 80 81 func mapView(_ mapView: MKMapView, renderFor overlay: MKOverlay) -> MKOverlayRenderer { 82 let myPolyLineRendere: MKPolylineRenderer = MKPolylineRenderer(overlay: overlay) 83 84 if("route" == overlay.subtitle){ 85 86 myPolyLineRendere.lineWidth = 6 87 88 myPolyLineRendere.strokeColor = UIColor.green 89 90 } 91 else{ 92 93 myPolyLineRendere.lineWidth = 5 94 95 myPolyLineRendere.strokeColor = UIColor.blue 96 } 97 98 return myPolyLineRendere 99 100 } 101 102 103} 104
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。