我正在尝试为MapView上的路线添加折线,从位置A到位置B(这是当前用户的GPS位置),因此路线/折线可以从设定的位置A跟随到任何地方用户即时(位置B)处于当前状态。
现在我的叠加层无效。我查看了MKPolylineView上的其他几个SO线程,但是当我尝试实现它们的代码时(下面也是如此),路由/行仍未显示。我是iOS的新手,所以我自己还在熟悉Swift / mapKit。
这是我到目前为止所做的:(经过修改以显示重要部分)
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var setLocButton: UIButton!
@IBOutlet weak var mapView: MKMapView!
var locationManager: CLLocationManager = CLLocationManager()
var carLat = 36.136111, carLong = -80.279462
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) { //AnyObject->CLLocation
var latestLocation: CLLocation = locations[locations.count - 1]
//Displaying location A on mapView
let carCoords = CLLocationCoordinate2D(latitude: carLat, longitude: carLong)
let region = MKCoordinateRegion(center: carCoords, span: MKCoordinateSpan(latitudeDelta: 0.0035, longitudeDelta: 0.0035))
mapView.mapType = MKMapType.Hybrid
mapView.setRegion(region, animated: true)
//Attempting to display route information on mapView
mapView.showsUserLocation = true
var locations = [CLLocation(latitude: carLat, longitude: carLong), CLLocation(latitude: latestLocation.coordinate.latitude, longitude: latestLocation.coordinate.latitude)]
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)
self.mapView.addOverlay(polyline)
}
//rendererForOverlay
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
/* Does not get called */
print("rendererForOverlay")
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blueColor()
polylineRenderer.lineWidth = 5
return polylineRenderer
}
return nil
}
我还发现了another example using MKDirections,这似乎更理想,因为它允许我设置传输类型(MKDirectionsTransportType.Walking)。我在使用这些说明绘制路线时遇到了问题。
使用第二组说明,这是解决Xcode提醒我的一些错误后得到的:
var route: MKRoute?
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) { //AnyObject->CLLocation
var latestLocation: CLLocation = locations[locations.count - 1] //AnyObject
/*
...
*/
//carLocation = Location A; currentLocation = location B
var directionsRequest = MKDirectionsRequest()
let carLocation = MKPlacemark(coordinate: carCoords, addressDictionary: nil)
var currentLocation = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: coorLat, longitude: coorLong), addressDictionary: nil)
directionsRequest.source = MKMapItem(placemark: currentLocation)
directionsRequest.destination = MKMapItem(placemark: carLocation)
directionsRequest.transportType = MKDirectionsTransportType.Walking
var directions = MKDirections(request: directionsRequest)
directions.calculateDirectionsWithCompletionHandler {
(response, error) -> Void in
if error == nil {
self.route = response!.routes[0] as? MKRoute
self.mapView.addOverlay((self.route?.polyline)!)
}
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
/* Does not get executed as well */
print("RendererForOverLay")
var myLineRenderer = MKPolylineRenderer(polyline: (route?.polyline)!)
myLineRenderer.strokeColor = UIColor.redColor()
myLineRenderer.lineWidth = 3
return myLineRenderer
}
我是否以某种方式将rendererForOverlay链接错误,因为在两个实例中都没有调用它?
答案 0 :(得分:0)
Sofacoder是对的,折线渲染器现在可以使用了!
我忘了在我的函数中添加 myMap.delegate = self ,并且还省略了 MKMapViewDelegate ViewController的声明:
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {