gpt4 book ai didi

ios - MKMapView 中的叠加 MKPolyLine 问题(rendererForOverLay)

转载 作者:行者123 更新时间:2023-11-28 08:46:13 25 4
gpt4 key购买 nike

我正在尝试为 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 链接错了,因为在这两种情况下都没有调用它?

最佳答案

Sofacoder 是对的,折线渲染器现在可以工作了!

我忘记在我的函数中添加 myMap.delegate = self,并且还省略了 MKMapViewDelegate ViewController 的声明:

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

关于ios - MKMapView 中的叠加 MKPolyLine 问题(rendererForOverLay),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35303113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com