gpt4 book ai didi

swift - 绘制路径有MKMapView问题

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

我正在尝试绘制一条路径(使用 MKMapView)。但是我有:

NSInvalidArgumentException ', reason:' *** - [NSMutableOrderedSet addObject:]: object can not be nil '.

我的这个按钮操作带有调用函数 stopNSTimer

@IBAction func startRoute(sender: UIButton) {
// Timer stop() function
// Each 4 seconds
timer = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: #selector(ViewController.stop), userInfo: nil, repeats: true)
}

这是我的 stop() 函数,它放置一个“起点”并将当前位置添加到类型为 的名为 points 的向量中>var points: [CLLocationCoordinate2D] = [] 并使用 addOverlay 绘制路径。

func stop() {
if (self.initialFlag == 0) {
// Put flag to 1
self.initialFlag = 1

// Add a Annotation
let sourceAnnotation = MKPointAnnotation()
sourceAnnotation.title = "Start Point"
sourceAnnotation.coordinate = (self.locationManager.location?.coordinate)!
self.mapView.showAnnotations([sourceAnnotation], animated: true)
}

// Get current point location
let currentLocation = CLLocationCoordinate2DMake((self.locationManager.location?.coordinate.latitude)!,(self.locationManager.location?.coordinate.longitude)!);

// We add points every 4 seconds then draw the map points
self.points.append(currentLocation)


// Draw the path

geodesic = MKGeodesicPolyline(coordinates: &points[0], count: points.count)

// And I have the error in this line below.
self.mapView.addOverlay(self.geodesic, level: .AboveRoads)

}

最佳答案

嘿,如果您没有足够的坐标来创建 MKGeodesicPolyline,它会抛出一个错误,我的解决方案就是它

if points.count > 1{
geodesic = MKGeodesicPolyline(coordinates: &points[0], count: points.count)
self.mapView.addOverlay(self.geodesic, level: .AboveRoads)
}

关于swift - 绘制路径有MKMapView问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37275605/

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