リンクの動画を見ながら、同じように設定しましたが、検索しようとするとエラーが発生し、シミュレータが止まります。
html
1 2import UIKit 3import MapKit 4import CoreLocation 5 6class ViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate { 7 8 @IBOutlet weak var textFieldForAddress: UITextField! 9 @IBOutlet weak var getDirectionsButton: UIButton! 10 @IBOutlet weak var map: MKMapView! 11 12 var locationManager = CLLocationManager() 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 locationManager.delegate = self 18 locationManager.desiredAccuracy = kCLLocationAccuracyBest 19 locationManager.requestAlwaysAuthorization() 20 locationManager.requestWhenInUseAuthorization() 21 locationManager.startUpdatingHeading() 22 23 map.delegate = self 24 25 } 26 27 @IBAction func getDirecitonsTapped(_ sender: Any) { 28 getAddress() 29 } 30 31 //緯度経度検索メゾット 32 func getAddress() { 33 34 let geoCoder = CLGeocoder() 35 geoCoder.geocodeAddressString(textFieldForAddress.text!) { (placemarks, error) in 36 guard let placemarks = placemarks, let location = placemarks.first?.location 37 else { 38 print("No Location Found") 39 return 40 } 41 print(location) 42 self.mapThis(destinationCord: location.coordinate) 43 } 44 } 45 46 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 47 print(locations) 48 } 49 func mapThis(destinationCord : CLLocationCoordinate2D) { 50 let sourceCordinate = (locationManager.location?.coordinate)! 51 52 let sourcePlaceMark = MKPlacemark(coordinate: sourceCordinate) 53 let destPlaceMark = MKPlacemark(coordinate: destinationCord) 54 55 let sourceItem = MKMapItem(placemark: sourcePlaceMark) 56 let destItem = MKMapItem(placemark: destPlaceMark) 57 58 let destinationRequest = MKDirections.Request() 59 destinationRequest.source = sourceItem 60 destinationRequest.destination = destItem 61 destinationRequest.transportType = .automobile 62 destinationRequest.requestsAlternateRoutes = true 63 64 let directions = MKDirections(request: destinationRequest) 65 directions.calculate { (response, error) in 66 guard let response = response else { 67 if let error = error{ 68 69 print("Something is wrong") 70 71 } 72 return 73 } 74 75 let route = response.routes[0] 76 self.map.addOverlay(route.polyline) 77 self.map.setVisibleMapRect(route.polyline.boundingMapRect, animated: true) 78 79 } 80 81 82 } 83 84 //Map上に線を引くメゾット 85 func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 86 let render = MKPolygonRenderer(overlay: overlay as! MKPolyline) 87 render.strokeColor = .blue 88 return render 89 } 90 91 92 93} 94
エラー箇所のポイント
html
1func mapThis(destinationCord : CLLocationCoordinate2D) { 2 let sourceCordinate = (locationManager.location?.coordinate)!
エラーの内容
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
状態①
コンソールへの表示(緯度経度、時間等)は問題ありません。
状態②
県名(Kyoto)を入れるとエラーでシミュレータが止まります。
状態③
マップ自体は正常で、移動、拡大縮小ができます。
以上、助言をよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/23 12:29
2020/06/23 20:57
2020/06/24 03:40
2020/06/24 05:31