为什么我的Polyline只画点不画线?
Why does my Polyline only draw a point and not draw a line?
我在我的 MapKit 地图上绘制折线时遇到问题,我应该尝试在用户要去的地方画一条线,问题是当我移动用户的位置时它只在新位置画点但不追踪完整的线
我已经试过用一个起点和一个起点画线,但它对我不起作用,假设如果用户走路他的位置更新并且他应该去画线
这是我的方法 didUpdateLocations:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last{
self.currentLocation = location
mapKit.centerCoordinate = location.coordinate
//Validamos si el usuario quiere recordar su ruta
if SettingsViewController.recordRutaOn{
points.append(location)
print("Estamos registrando la ruta del usuario")
}
//Prueba para pintar la ruta
print((locations.last?.coordinate.latitude)!,(locations.last?.coordinate.longitude)!)
var rutaPoly = CLLocationCoordinate2D(latitude: (locations.last?.coordinate.latitude)!, longitude: (locations.last?.coordinate.longitude)!)
let polyline = MKPolyline(coordinates: &rutaPoly, count: locations.count)
mapKit.add(polyline)
if self.currentLocation == nil{
self.currentAnnotation = MKPointAnnotation()
self.currentAnnotation?.coordinate = location.coordinate
self.mapKit.addAnnotation(self.currentAnnotation!)
}
}
}
这是我的 renderFor 方法:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolyline{
let polyline = overlay
let polyLineRender = MKPolylineRenderer(overlay: polyline)
polyLineRender.strokeColor = UIColor.blue
polyLineRender.lineWidth = 2.0
return polyLineRender
}
return MKPolylineRenderer()
}
我不明白我做错了什么,只画了一个点
您已将 rutaPoly
定义为单个 CLLocationCoordinate2D
。因此,当您从中创建 MKPolyline
时,只有一个坐标,因此渲染的多段线将是一个点。更糟糕的是,您提供给 MKPolyline
的 count
参数与第一个参数无关,您可以通过这种方式获得一些未定义的行为。
您想保留一个坐标数组并将您的新坐标附加到该数组。类似于:
var coordinates: [CLLocationCoordinate2D] = []
var currentPolyline: MKPolyline?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
coordinates.append(location.coordinate)
let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(polyline)
if let currentPolyline = currentPolyline {
mapView.removeOverlay(currentPolyline)
}
currentPolyline = polyline
}
或者,您也可以将每个新的位置更新添加为单独的多段线:
var previousCoordinate: CLLocationCoordinate2D?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
if let previousCoordinate = previousCoordinate {
let coordinates = [previousCoordinate, location.coordinate]
let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(polyline)
}
previousCoordinate = location.coordinate
}
但是无论您做什么,请确保为 MKPolyline
初始值设定项提供了一个 CLLocationCoordinate2D
数组及其相应的 count
.
我已经试过用一个起点和一个起点画线,但它对我不起作用,假设如果用户走路他的位置更新并且他应该去画线
这是我的方法 didUpdateLocations:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last{
self.currentLocation = location
mapKit.centerCoordinate = location.coordinate
//Validamos si el usuario quiere recordar su ruta
if SettingsViewController.recordRutaOn{
points.append(location)
print("Estamos registrando la ruta del usuario")
}
//Prueba para pintar la ruta
print((locations.last?.coordinate.latitude)!,(locations.last?.coordinate.longitude)!)
var rutaPoly = CLLocationCoordinate2D(latitude: (locations.last?.coordinate.latitude)!, longitude: (locations.last?.coordinate.longitude)!)
let polyline = MKPolyline(coordinates: &rutaPoly, count: locations.count)
mapKit.add(polyline)
if self.currentLocation == nil{
self.currentAnnotation = MKPointAnnotation()
self.currentAnnotation?.coordinate = location.coordinate
self.mapKit.addAnnotation(self.currentAnnotation!)
}
}
}
这是我的 renderFor 方法:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolyline{
let polyline = overlay
let polyLineRender = MKPolylineRenderer(overlay: polyline)
polyLineRender.strokeColor = UIColor.blue
polyLineRender.lineWidth = 2.0
return polyLineRender
}
return MKPolylineRenderer()
}
我不明白我做错了什么,只画了一个点
您已将 rutaPoly
定义为单个 CLLocationCoordinate2D
。因此,当您从中创建 MKPolyline
时,只有一个坐标,因此渲染的多段线将是一个点。更糟糕的是,您提供给 MKPolyline
的 count
参数与第一个参数无关,您可以通过这种方式获得一些未定义的行为。
您想保留一个坐标数组并将您的新坐标附加到该数组。类似于:
var coordinates: [CLLocationCoordinate2D] = []
var currentPolyline: MKPolyline?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
coordinates.append(location.coordinate)
let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(polyline)
if let currentPolyline = currentPolyline {
mapView.removeOverlay(currentPolyline)
}
currentPolyline = polyline
}
或者,您也可以将每个新的位置更新添加为单独的多段线:
var previousCoordinate: CLLocationCoordinate2D?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
if let previousCoordinate = previousCoordinate {
let coordinates = [previousCoordinate, location.coordinate]
let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(polyline)
}
previousCoordinate = location.coordinate
}
但是无论您做什么,请确保为 MKPolyline
初始值设定项提供了一个 CLLocationCoordinate2D
数组及其相应的 count
.