我在地图上画了MKPolyline。有时当我缩放几次时,地图上会留下这样的东西:
对于iOS地图而言,我无法改进吗?
或者也许我的绘图方法出了问题?
我的MKPolyline课程:
class MulticolorPolylineSegment: MKPolyline {
var color: UIColor?
class func colorSegments(forLocations locations: [Location]) -> [MulticolorPolylineSegment] {
var colorSegments = [MulticolorPolylineSegment]()
let red = (r: 254.0/255.0, g: 200.0 / 255.0, b: 20.0/255.0)
// RGB for Yellow (middle)
let yellow = (r: 254.0/255.0, g: 200.0 / 255.0, b: 20.0/255.0)
// RGB for Green (fastest)
let green = (r: 254.0/255.0, g: 200.0 / 255.0, b: 20.0/255.0)
let (speeds, minSpeed, maxSpeed) = allSpeeds(forLocations: locations)
// now knowing the slowest+fastest, we can get mean too
let meanSpeed = (minSpeed + maxSpeed)/2
for i in 1..<locations.count {
let l1 = locations[i-1]
let l2 = locations[i]
print("dsd")
var coords = [CLLocationCoordinate2D]()
coords.append(CLLocationCoordinate2D(latitude: l1.latitude, longitude: l1.longitude))
coords.append(CLLocationCoordinate2D(latitude: l2.latitude, longitude: l2.longitude))
let speed = speeds[i-1]
var color = UIColor.black
if speed < minSpeed { // Between Red & Yellow
let ratio = (speed - minSpeed) / (meanSpeed - minSpeed)
let r = CGFloat(red.r + ratio * (yellow.r - red.r))
let g = CGFloat(red.g + ratio * (yellow.g - red.g))
let b = CGFloat(red.r + ratio * (yellow.r - red.r))
color = UIColor(red: r, green: g, blue: b, alpha: 1)
}
else { // Between Yellow & Green
let ratio = (speed - meanSpeed) / (maxSpeed - meanSpeed)
let r = CGFloat(yellow.r + ratio * (green.r - yellow.r))
let g = CGFloat(yellow.g + ratio * (green.g - yellow.g))
let b = CGFloat(yellow.b + ratio * (green.b - yellow.b))
color = UIColor(red: r, green: g, blue: b, alpha: 1)
}
let segment = MulticolorPolylineSegment(coordinates: &coords, count: coords.count)
segment.color = color
colorSegments.append(segment)
}
return colorSegments
}
fileprivate class func allSpeeds(forLocations locations: [Location]) -> (speeds: [Double], minSpeed: Double, maxSpeed: Double) {
// Make Array of all speeds. Find slowest and fastest
var speeds = [Double]()
var minSpeed = DBL_MAX
var maxSpeed = 0.0
for i in 1..<locations.count {
let l1 = locations[i-1]
let l2 = locations[i]
let cl1 = CLLocation(latitude: l1.latitude, longitude: l1.longitude)
let cl2 = CLLocation(latitude: l2.latitude, longitude: l2.longitude)
let distance = cl2.distance(from: cl1)
let time = l2.timestamp.timeIntervalSince(l1.timestamp as Date)
let speed = distance/time
minSpeed = min(minSpeed, speed)
maxSpeed = max(maxSpeed, speed)
speeds.append(speed)
}
return (speeds, minSpeed, maxSpeed)
}
}
查看控制器方法实施
@objc(mapView:rendererForOverlay:) func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let polyline = overlay as! MulticolorPolylineSegment
let renderer = MKPolylineRenderer(polyline: polyline)
renderer.strokeColor = polyline.color
renderer.lineWidth = 10
return renderer
}
func polyline() -> MKPolyline {
var coords = [CLLocationCoordinate2D]()
let locations = self.locations
for location in locations {
coords.append(CLLocationCoordinate2D(latitude: location.latitude,
longitude: location.longitude))
print("dodalo")
}
return MKPolyline(coordinates: &coords, count: run.locations.count)
}