0
私はある種の方向/ GPSアプリケーションを作成しています。私は彼らがしかし Swift 2.0のiOSアプリにMKPolyLineが表示されない
素早く簡単に目的地を設定することができるように管理してきた私は
- 私は小さな問題にぶつかってきました。私が欲しいのは、ユーザーが画面上の目的地を選択することであり、アプリケーションはそこに到着する最も速い方法を提供するでしょう。ここで
は私のViewControllerです:
class ViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate { @IBOutlet var mapOfMaps: MKMapView! let locationManager = CLLocationManager() var center:CLLocationCoordinate2D! var SourcePM:MKPlacemark! let sourcePlacemark: MKPlacemark! = nil override func viewDidLoad() { super.viewDidLoad() self.locationManager.delegate = self self.locationManager.desiredAccuracy = kCLLocationAccuracyBest self.locationManager.requestWhenInUseAuthorization() self.locationManager.startUpdatingLocation() self.mapOfMaps.showsUserLocation = true let sourceAnnotation = MKPointAnnotation() if let location = locationManager.location { sourceAnnotation.coordinate = location.coordinate } let longPress = UILongPressGestureRecognizer(target: self, action: #selector(CardioViewController.action(_:))) longPress.minimumPressDuration = 1.0 mapOfMaps.addGestureRecognizer(longPress) //directionRequest } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let location = locations.last center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.003, longitudeDelta: 0.003)) self.mapOfMaps.setRegion(region, animated: true) self.locationManager.stopUpdatingLocation() SourcePM = MKPlacemark(coordinate: center, addressDictionary: nil) } func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { print("ERROr " + error.localizedDescription) } func action(gestureRecognizer:UIGestureRecognizer) { let touchPoint = gestureRecognizer.locationInView(self.mapOfMaps) let newCoord:CLLocationCoordinate2D = mapOfMaps.convertPoint(touchPoint, toCoordinateFromView: self.mapOfMaps) let newAnotation = MKPointAnnotation() newAnotation.coordinate = newCoord newAnotation.title = "Your Destination" mapOfMaps.addAnnotation(newAnotation) let anotPM = MKPlacemark(coordinate: newAnotation.coordinate, addressDictionary: nil) let source = MKMapItem(placemark: SourcePM) let dstn = MKMapItem(placemark: anotPM) let directionRequest = MKDirectionsRequest() directionRequest.source = source directionRequest.destination = dstn directionRequest.transportType = .Automobile // Calculate the direction let directions = MKDirections(request: directionRequest) // 8. directions.calculateDirectionsWithCompletionHandler() { (response, error) in if(error == nil && response != nil) { for route in response!.routes { var r: MKRoute = route as! MKRoute self.mapOfMaps.addOverlay(r.polyline, level: MKOverlayLevel.AboveRoads) } } let route = response!.routes[0] self.mapOfMaps.addOverlay((route.polyline), level: MKOverlayLevel.AboveLabels) let rect = route.polyline.boundingMapRect self.mapOfMaps.setRegion(MKCoordinateRegionForMapRect(rect), animated: true) } } func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer { let renderer = MKPolylineRenderer(overlay: overlay) renderer.strokeColor = UIColor.orangeColor() renderer.alpha = 1 renderer.lineWidth = 4.0 return renderer } }
私のコードを通してあなたを歩いて支援します。私のユーザーは、マップ上の目的地を押し続け、アプリは注釈を追加し、はの経路を指定する必要があります。ただし、アノテーションがマップに追加され、マップは両方の場所(ユーザーの場所と注釈の場所)を表示するよう調整されますが、ルートは表示されません。