Swift、MKPolyline如何在坐标点之间创建折线
Swift, MKPolyline how to create polyline between coordinate point
嗨,我写了这段代码来在一些点之间画一条折线:
var arrayToDraw: Array<Any> = []
var forpolyline: Array<CLLocationDegrees> = []
var forpolyline2: CLLocationCoordinate2D = CLLocationCoordinate2D.init()
func showRoute() {
for h in 0...(orderFinalDictionary.count - 1){
arrayToDraw = orderFinalDictionary[h].value
print(arrayToDraw)
var arrayToDrawCount = arrayToDraw.count
for n in 0...(arrayToDrawCount - 1){
forpolyline = (arrayToDraw[n] as! Array<CLLocationDegrees>)
forpolyline2.latitude = (forpolyline[0])
forpolyline2.longitude = (forpolyline[1])
print(forpolyline2)
var geodesic = MKPolyline(coordinates: &forpolyline2, count: 1)
self.mapView.add(geodesic)
}
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 3
return renderer
}
它从字典中获取坐标,将其放入数组 (arraToDraw) 中,然后我使用 forpolyline 和 forpolyline2 来转换值。
现在的问题是它只在坐标上画点,我该如何连接它?
您正在创建具有单个点的多条折线,而不是具有多个点的单个折线。在不知道你的字典结构的情况下很难把代码写对,但这应该更符合你正在尝试做的事情:
var arrayToDraw: Array<Any> = []
var forpolyline: Array<CLLocationDegrees> = []
var forpolyline2: CLLocationCoordinate2D = [CLLocationCoordinate2D]()
func showRoute() {
for h in 0...(orderFinalDictionary.count - 1){
arrayToDraw = orderFinalDictionary[h].value
print(arrayToDraw)
var arrayToDrawCount = arrayToDraw.count
for n in 0...(arrayToDrawCount - 1){
forpolyline = (arrayToDraw[n] as! Array<CLLocationDegrees>)
forpolyline2.append(CLLocationCoordinate2D(latitude: forpolyline[0], longitude: forpolyline[1]))
}
print(forpolyline2)
let geodesic = MKPolyline(coordinates: &forpolyline2, count: forpolyline2.count)
self.mapView.add(geodesic)
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 3
return renderer
}
嗨,我写了这段代码来在一些点之间画一条折线:
var arrayToDraw: Array<Any> = []
var forpolyline: Array<CLLocationDegrees> = []
var forpolyline2: CLLocationCoordinate2D = CLLocationCoordinate2D.init()
func showRoute() {
for h in 0...(orderFinalDictionary.count - 1){
arrayToDraw = orderFinalDictionary[h].value
print(arrayToDraw)
var arrayToDrawCount = arrayToDraw.count
for n in 0...(arrayToDrawCount - 1){
forpolyline = (arrayToDraw[n] as! Array<CLLocationDegrees>)
forpolyline2.latitude = (forpolyline[0])
forpolyline2.longitude = (forpolyline[1])
print(forpolyline2)
var geodesic = MKPolyline(coordinates: &forpolyline2, count: 1)
self.mapView.add(geodesic)
}
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 3
return renderer
}
它从字典中获取坐标,将其放入数组 (arraToDraw) 中,然后我使用 forpolyline 和 forpolyline2 来转换值。
现在的问题是它只在坐标上画点,我该如何连接它?
您正在创建具有单个点的多条折线,而不是具有多个点的单个折线。在不知道你的字典结构的情况下很难把代码写对,但这应该更符合你正在尝试做的事情:
var arrayToDraw: Array<Any> = []
var forpolyline: Array<CLLocationDegrees> = []
var forpolyline2: CLLocationCoordinate2D = [CLLocationCoordinate2D]()
func showRoute() {
for h in 0...(orderFinalDictionary.count - 1){
arrayToDraw = orderFinalDictionary[h].value
print(arrayToDraw)
var arrayToDrawCount = arrayToDraw.count
for n in 0...(arrayToDrawCount - 1){
forpolyline = (arrayToDraw[n] as! Array<CLLocationDegrees>)
forpolyline2.append(CLLocationCoordinate2D(latitude: forpolyline[0], longitude: forpolyline[1]))
}
print(forpolyline2)
let geodesic = MKPolyline(coordinates: &forpolyline2, count: forpolyline2.count)
self.mapView.add(geodesic)
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 3
return renderer
}