【发布时间】:2018-10-01 07:31:23
【问题描述】:
我需要在 MKPolyline 上找到等距的位置来添加注释,如下所示。
我在 MKPolyline 上获取位置的函数如下所示,我有折线的开始和结束坐标值。但是位置稍微有些偏离折线,如下图所示
我查找位置的功能是
func getEquidistantPoints(from startPoint: CLLocationCoordinate2D, to endPoint: CLLocationCoordinate2D, numberOfPoints: Int) -> [CLLocationCoordinate2D] {
var midPoints: [CLLocationCoordinate2D] = []
var newPoint: CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0)
let count = numberOfPoints + 1
let latitudeModifier = (endPoint.latitude - startPoint.latitude) / Double(count)
let longitudeModifier = (endPoint.longitude - startPoint.longitude) / Double(count)
for i in 0..<count {
newPoint.latitude = CLLocationDegrees(startPoint.latitude + (latitudeModifier * Double(i)))
newPoint.longitude = CLLocationDegrees(startPoint.longitude + (longitudeModifier * Double(i)))
midPoints.append(newPoint)
}
return midPoints
}
在viewdidload中
let coordinatesArray = getEquidistantPoints(from: sourceCoordinate, to: destinationCoordinate, numberOfPoints: 5)
for coordinate in coordinatesArray {
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
self.mapView.addAnnotation(annotation)
}
如何解决这个位置计算错误?
【问题讨论】:
标签: ios swift google-maps google-maps-api-3 mkmapview