GradientPolylineOverlay

import Foundation
import MapKit

class GradientPolylineOverlay: NSObject, MKOverlay {
var points: [MKMapPoint] = []
var _pointCount: Int = 0
var pointCount: Int {
return _pointCount
}
var pointSpace: Int = 0
var _boundingMapRect: MKMapRect
var velocity: [CGFloat] = []

var rwLock: pthread_rwlock_t = pthread_rwlock_t()

let initialPointSpace: Int = 1000
let minimumDeltaMeters: Double = 10

init(coord: CLLocationCoordinate2D) {
    
    pointSpace = initialPointSpace
    points.append(MKMapPointForCoordinate(coord))
    _pointCount = 1
    
    var origin: MKMapPoint = points[0]
    origin.x -= MKMapSizeWorld.width / 8.0
    origin.y -= MKMapSizeWorld.height / 8.0
    var size: MKMapSize = MKMapSizeWorld
    size.width /= 4.0
    size.height /= 4.0
    _boundingMapRect = MKMapRect(origin: origin, size: size)
    let worldRect: MKMapRect = MKMapRectMake(0, 0, MKMapSizeWorld.width, MKMapSizeWorld.height)
    _boundingMapRect = MKMapRectIntersection(_boundingMapRect, worldRect)
    
    super.init()
    pthread_rwlock_init(&rwLock, nil)
}

init(_points: [CLLocationCoordinate2D], _velocity: [CGFloat], _count: Int) {
    
    _pointCount = _count
    for point in _points {
        self.points.append(MKMapPointForCoordinate(point))
    }
    
    for velocity in _velocity {
        self.velocity.append(velocity)
    }
    
    //bite off up to 1/4 of the world to draw into
    var origin: MKMapPoint = points[0]
    origin.x -= MKMapSizeWorld.width / 8.0
    origin.y -= MKMapSizeWorld.height / 8.0
    var size: MKMapSize = MKMapSizeWorld
    size.width /= 4.0
    size.height /= 4.0
    _boundingMapRect = MKMapRect(origin: origin, size: size)
    let worldRect = MKMapRectMake(0, 0, MKMapSizeWorld.width, MKMapSizeWorld.height)
    _boundingMapRect = MKMapRectIntersection(_boundingMapRect, worldRect)
    
    super.init()
    pthread_rwlock_init(&rwLock, nil)
}

deinit {
    points.removeAll()
    velocity.removeAll()
    pthread_rwlock_destroy(&rwLock)
}

func getBoundingMapRect() -> MKMapRect{
    return _boundingMapRect
}

func addCoordinate(coord: CLLocationCoordinate2D) -> MKMapRect {
    pthread_rwlock_wrlock(&rwLock)
    //Convert a CLLocationCoordinate2D to an MKMapPoint
    let newPoint: MKMapPoint = MKMapPointForCoordinate(coord)
    let prevPoint: MKMapPoint = points[_pointCount-1]
    
    //Get the distance between this new point and previous point
    let  metersApart: CLLocationDistance = MKMetersBetweenMapPoints(newPoint, prevPoint)
    var updateRect: MKMapRect = MKMapRectNull
    
    if (metersApart > minimumDeltaMeters){
        //Grow the points array if necessary
        if (pointSpace == _pointCount){
            pointSpace *= 2
        }
        
        //Add the new point to points array
        points[_pointCount] = newPoint
        _pointCount += 1
        
        //Compute MKMapRect bounding prevPoint and newPoint
        let minX: Double = (newPoint.x > prevPoint.x) ? prevPoint.x : newPoint.x
        let minY: Double = (newPoint.y > prevPoint.y) ? prevPoint.y : newPoint.y
        let maxX: Double = (newPoint.x > prevPoint.x) ? newPoint.x : prevPoint.x
        let maxY: Double = (newPoint.y > prevPoint.y) ? newPoint.y : prevPoint.y
        
        updateRect = MKMapRectMake(minX, minY, maxX - minX, maxY - minY)
    }
    
    pthread_rwlock_unlock(&rwLock)
    return updateRect
}

var boundingMapRect: MKMapRect {
    return _boundingMapRect
}

var coordinate: CLLocationCoordinate2D {
    return MKCoordinateForMapPoint(points[0])
}

func lockForReading() {
    pthread_rwlock_rdlock(&rwLock)
}

func unlockForReading() {
    pthread_rwlock_unlock(&rwLock)
}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile丽语阅读 9,274评论 0 6
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,865评论 0 9
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,800评论 19 139
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 4,814评论 1 6
  • 今天,我和燕姐一起到天津参加了2017年化氏核心战略合作伙伴峰会暨2018新品发布会。如下是参会总结,供各位参考:...
    Carlyle6666阅读 7,749评论 0 0

友情链接更多精彩内容