我正在尝试在地图上绘制MKPolyline
。当我在应用程序上进行模拟运行时,位置会正确移动,但不会画线。我该如何解决这个问题?
mapView.delegate = self
mapView.showsUserLocation = true
mapView.mapType = MKMapType(rawValue: 0)!
mapView.userTrackingMode = MKUserTrackingMode(rawValue: 2)!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
mapView.mapType = MKMapType(rawValue: 0)!
}
override func viewWillAppear(_ animated: Bool) {
locationManager.startUpdatingHeading()
locationManager.startUpdatingLocation()
}
override func viewWillDisappear(_ animated: Bool) {
locationManager.stopUpdatingHeading()
locationManager.stopUpdatingLocation()
}
// MARK: - CLLocationManager delegate
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
//drawing path or route covered
if let oldLocationNew = oldLocation as CLLocation?{
let oldCoordinates = oldLocationNew.coordinate
let newCoordinates = newLocation.coordinate
var area = [oldCoordinates, newCoordinates]
var polyline = MKPolyline(coordinates: &area, count: area.count)
mapView.add(polyline)
}
//calculation for location selection for pointing annoation
if let previousLocationNew = previousLocation as CLLocation?{
//case if previous location exists
if previousLocation.distance(from: newLocation) > 200 {
addAnnotationsOnMap(locationToPoint: newLocation)
previousLocation = newLocation
}
}
else{
//case if previous location doesn't exists
addAnnotationsOnMap(locationToPoint: newLocation)
previousLocation = newLocation
}
}
// MARK: - MKMapView delegate
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if (overlay is MKPolyline) {
var pr = MKPolylineRenderer(overlay: overlay)
pr.strokeColor = UIColor.red
pr.lineWidth = 5
return pr
}
return nil
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if startDate == nil {
startDate = Date()
} else {
print("elapsedTime:", String(format: "%.0fs", Date().timeIntervalSince(startDate)))
timeLabel.text="\(Date().timeIntervalSince(startDate))"
}
if startLocation == nil {
startLocation = locations.first
} else if let location = locations.last {
traveledDistance += lastLocation.distance(from: location)
print("Traveled Distance:", traveledDistance)
distanceLabel.text="\(traveledDistance)"
print("Straight Distance:", startLocation.distance(from: locations.last!))
}
lastLocation = locations.last
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
if (error as? CLError)?.code == .denied {
manager.stopUpdatingLocation()
manager.stopMonitoringSignificantLocationChanges()
}
}
MKPolyline
应该在用户移动时绘制。
答案 0 :(得分:0)
mapView(_:rendererFor:)
的签名不正确。它已经改变了。现在是:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = .red
renderer.lineWidth = 5
return renderer
}
fatalError("Unexpected MKOverlay type")
}
如果您在当前方法中添加print
语句或断点,我相信您会发现它没有被调用。而且,当然,请确保已在IB或以编程方式设置了地图视图的delegate
。
顺便说一句,didUpdateToLocation
也有类似的问题。现在的签名是:
func locationManager(_ manager: CLLocationManager,
didUpdateTo newLocation: CLLocation,
from oldLocation: CLLocation) {
...
}
但是您根本不应该使用该方法。如您在documentation中所看到的,它已被弃用。请改用locationManager(_:didUpdateLocations:)
。您必须保持自己对oldLocation
var oldLocation: CLLocation?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
if let oldCoordinate = oldLocation?.coordinate {
let coordinates = [oldCoordinate, location.coordinate]
let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(polyline)
}
oldLocation = location
...
}