在mapbox gl中为ios创建一个圆圈

时间:2015-07-12 04:42:37

标签: ios swift mapbox-gl

RMCircle似乎没有在swift中定义,我该如何绘制一个简单的圆圈?

    var circle = RMCircle(position: position, radius: 3000)

相反,我只有MLGShape和MLGPolygon,但没有MLGCircle

1 个答案:

答案 0 :(得分:6)

http://github.com/mapbox/mapbox-gl-native/issues/2167

上提供的解决方法

将解决方案重写为 swift 2.0

func polygonCircleForCoordinate(coordinate: CLLocationCoordinate2D, withMeterRadius: Double) {
    let degreesBetweenPoints = 8.0
    //45 sides
    let numberOfPoints = floor(360.0 / degreesBetweenPoints)
    let distRadians: Double = withMeterRadius / 6371000.0
    // earth radius in meters
    let centerLatRadians: Double = coordinate.latitude * M_PI / 180
    let centerLonRadians: Double = coordinate.longitude * M_PI / 180
    var coordinates = [CLLocationCoordinate2D]()
    //array to hold all the points
    for var index = 0; index < Int(numberOfPoints); index++ {
        let degrees: Double = Double(index) * Double(degreesBetweenPoints)
        let degreeRadians: Double = degrees * M_PI / 180
        let pointLatRadians: Double = asin(sin(centerLatRadians) * cos(distRadians) + cos(centerLatRadians) * sin(distRadians) * cos(degreeRadians))
        let pointLonRadians: Double = centerLonRadians + atan2(sin(degreeRadians) * sin(distRadians) * cos(centerLatRadians), cos(distRadians) - sin(centerLatRadians) * sin(pointLatRadians))
        let pointLat: Double = pointLatRadians * 180 / M_PI
        let pointLon: Double = pointLonRadians * 180 / M_PI
        let point: CLLocationCoordinate2D = CLLocationCoordinate2DMake(pointLat, pointLon)
        coordinates.append(point)
    }
    let polygon = MGLPolygon(coordinates: &coordinates, count: UInt(coordinates.count))
    self.mapView.addAnnotation(polygon)
}

swift 3.0

func polygonCircleForCoordinate(coordinate: CLLocationCoordinate2D, withMeterRadius: Double) {
    let degreesBetweenPoints = 8.0
    //45 sides
    let numberOfPoints = floor(360.0 / degreesBetweenPoints)
    let distRadians: Double = withMeterRadius / 6371000.0
    // earth radius in meters
    let centerLatRadians: Double = coordinate.latitude * Double.pi / 180
    let centerLonRadians: Double = coordinate.longitude * Double.pi / 180
    var coordinates = [CLLocationCoordinate2D]()
    //array to hold all the points
    for index in 0 ..< Int(numberOfPoints) {
        let degrees: Double = Double(index) * Double(degreesBetweenPoints)
        let degreeRadians: Double = degrees * Double.pi / 180
        let pointLatRadians: Double = asin(sin(centerLatRadians) * cos(distRadians) + cos(centerLatRadians) * sin(distRadians) * cos(degreeRadians))
        let pointLonRadians: Double = centerLonRadians + atan2(sin(degreeRadians) * sin(distRadians) * cos(centerLatRadians), cos(distRadians) - sin(centerLatRadians) * sin(pointLatRadians))
        let pointLat: Double = pointLatRadians * 180 / Double.pi
        let pointLon: Double = pointLonRadians * 180 / Double.pi
        let point: CLLocationCoordinate2D = CLLocationCoordinate2DMake(pointLat, pointLon)
        coordinates.append(point)
    }
    let polygon = MGLPolygon(coordinates: &coordinates, count: UInt(coordinates.count))
    self.mapView.addAnnotation(polygon)
}