MKPolyline 未在地图上绘制。我该如何解决这个问题?
MKPolyline isn't drawing on the map. How can I resolve this issue?
我正在尝试在地图上绘制 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
应在用户移动时绘制。
mapView(_:rendererFor:)
的签名不正确。它已经改变了。现在是:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
switch overlay {
case let polyline as MKPolyline:
let renderer = MKPolylineRenderer(polyline: polyline)
renderer.strokeColor = .red
renderer.lineWidth = 5
return renderer
// you can add more `case`s for other overlay types as needed
default:
fatalError("Unexpected MKOverlay type")
}
}
如果你在你当前的方法里面加了一个print
语句或者断点,相信你会发现它没有被调用。当然,请确保您已经在 IB 中或以编程方式设置了地图视图的 delegate
。
顺便说一句,旧的locationManager(_:didUpdateTo:from:)
is deprecated. Use locationManager(_:didUpdateLocations:)
代替。您必须维护自己对 savedLocation
和 savedPolyline
:
的引用
var savedLocation: CLLocation?
var savedPolyline: MKPolyline?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last(where: { [=11=].horizontalAccuracy >= 0 }) else { return }
var polyline: MKPolyline?
if let oldCoordinate = savedLocation?.coordinate {
let coordinates = [oldCoordinate, location.coordinate]
polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(polyline!)
}
// if you want to remove the old one
//
// if let savedPolyline = savedPolyline {
// mapView.removeOverlay(savedPolyline)
// }
//
// savedPolyline = polyline
savedLocation = location
// ...
}
我正在尝试在地图上绘制 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
应在用户移动时绘制。
mapView(_:rendererFor:)
的签名不正确。它已经改变了。现在是:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
switch overlay {
case let polyline as MKPolyline:
let renderer = MKPolylineRenderer(polyline: polyline)
renderer.strokeColor = .red
renderer.lineWidth = 5
return renderer
// you can add more `case`s for other overlay types as needed
default:
fatalError("Unexpected MKOverlay type")
}
}
如果你在你当前的方法里面加了一个print
语句或者断点,相信你会发现它没有被调用。当然,请确保您已经在 IB 中或以编程方式设置了地图视图的 delegate
。
顺便说一句,旧的locationManager(_:didUpdateTo:from:)
is deprecated. Use locationManager(_:didUpdateLocations:)
代替。您必须维护自己对 savedLocation
和 savedPolyline
:
var savedLocation: CLLocation?
var savedPolyline: MKPolyline?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last(where: { [=11=].horizontalAccuracy >= 0 }) else { return }
var polyline: MKPolyline?
if let oldCoordinate = savedLocation?.coordinate {
let coordinates = [oldCoordinate, location.coordinate]
polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(polyline!)
}
// if you want to remove the old one
//
// if let savedPolyline = savedPolyline {
// mapView.removeOverlay(savedPolyline)
// }
//
// savedPolyline = polyline
savedLocation = location
// ...
}