gpt4 book ai didi

swift - 为什么 MKPolyline 没有出现在我的应用程序中?

转载 作者:行者123 更新时间:2023-11-28 06:23:56 26 4
gpt4 key购买 nike

我正在尝试创建一个应用程序,其功能之一是在用户移动时画线。

这是类

class traceuserViewController: UIViewController,CLLocationManagerDelegate, MKMapViewDelegate {
var locationManager = CLLocationManager()
var startLocation: CLLocation?
var endLocation: CLLocation?
@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.distanceFilter = 30.0
self.locationManager.startMonitoringSignificantLocationChanges()
self.locationManager.startUpdatingLocation()
mapView.showsUserLocation = true
mapView.mapType = .hybrid
self.mapView.delegate = self
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//user's current location
let nowlocation = locations.last
userLocations.append(nowlocation!)

print("HERE IS THE LOCATION ARRAY")
print(userLocations)

//show the current location region
let center = CLLocationCoordinate2D(latitude: nowlocation!.coordinate.latitude, longitude: nowlocation!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.7, longitudeDelta: 0.7))
self.mapView.setRegion(region, animated: true)
drawRoute(locationArray: userLocations)
}
func drawRoute(locationArray: [CLLocation]) {
if (locationArray.count) > 1 {
var destinationLocIndex = (locationArray.count) - 1
var startLocIndex = (locationArray.count) - 2

let destinationloc = locationArray[destinationLocIndex].coordinate
let startLoc = locationArray[startLocIndex].coordinate

var routeArray = [startLoc, destinationloc]
//test if the function works well or not
print(routeArray)
var geodesicLine = MKGeodesicPolyline(coordinates: routeArray , count: routeArray.count)
mapView.add(geodesicLine, level: .aboveRoads)

}
}

//draw in the mapview
private func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer! {
if overlay is MKPolyline{
let polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blue
polylineRenderer.lineWidth = 5.0
return polylineRenderer
}else{
os_log("Failed to draw the polyline", log: OSLog.default, type: .debug)
return nil
}
}

经过多次尝试,我仍然不知道为什么当用户移动时它没有在 map 上绘制路线,谁能给我一些提示吗?

干杯

最佳答案

我从代码片段中推断您正在使用 Swift 3(例如 didUpdateLocations 的签名;使用 .hybrid 而不是 Swift 2.3 的 .Hybrid; 等)。

但是,mapView(_:rendererFor:) 的签名是不正确的。在 Swift 3 中,它是:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
...
}

如果你有一个似乎不起作用的委托(delegate)方法,在其中添加一个断点,你可以确认它是否被调用(如果被调用,你可以单步执行它并诊断问题进一步)。

关于swift - 为什么 MKPolyline 没有出现在我的应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42563404/

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