Я пытаюсь добавить MKOverlay (мой MKRoute) в свой MapView, но я не могу понять, что я делаю неправильно. Это довольно неряшливо, потому что я экспериментирую с ним, прежде чем реализовать его в своем приложении.Я не могу заставить мой MKRoute отображаться на MapView
Вот код:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var map: MKMapView!
var geocoder:CLGeocoder = CLGeocoder()
var location:CLLocation = CLLocation(latitude: 38, longitude: -77)
var next:CLLocation = CLLocation(latitude: 38.9, longitude: -77.61)
var locMark:MKPlacemark?
var destMark:MKPlacemark?
var manager:CLLocationManager = CLLocationManager()
var source:MKMapItem?
var destination:MKMapItem?
var request:MKDirectionsRequest = MKDirectionsRequest()
var directions:MKDirections = MKDirections()
var directionsResponse:MKDirectionsResponse = MKDirectionsResponse()
var route:MKRoute! = MKRoute()
override func viewDidLoad() {
super.viewDidLoad()
self.map.delegate = self
manager.requestAlwaysAuthorization()
let latDelt = 0.05
let lonDelt = 0.05
let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latDelt, longitudeDelta: lonDelt)
let region:MKCoordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), span: span)
map.setRegion(region, animated: true)
map.mapType = MKMapType.Satellite
locMark = MKPlacemark(coordinate: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), addressDictionary: nil)
destMark = MKPlacemark(coordinate: CLLocationCoordinate2DMake(next.coordinate.latitude, next.coordinate.longitude), addressDictionary: nil)
source = MKMapItem(placemark: locMark)
destination = MKMapItem(placemark: destMark)
request.setSource(source)
request.setDestination(destination)
request.transportType = MKDirectionsTransportType.Automobile
request.requestsAlternateRoutes = true
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler { (response:MKDirectionsResponse?, error:NSError?) -> Void in
if error == nil {
self.directionsResponse = response!
self.route = self.directionsResponse.routes[0] as! MKRoute
self.map.addOverlay(self.route.polyline)
} else {
println(error)
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
var pr = MKPolylineRenderer(overlay: overlay)
pr.strokeColor = UIColor.purpleColor()
pr.lineWidth = 5
return pr
}
return nil
}
}
я не получаю какие-либо ошибки печататься, но не маршрут отображается на карте вообще.
я мог буквально плакать прямо сейчас в радости. Вообще-то, я немного плачу. Вы решили это. Теперь я не на 100% конкретный с концепцией, но я могу отработать этот рабочий код. Большое вам спасибо, вы закончили мой четырехдневный поход Google. – MantisShrimp