Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a circle in mapbox gl for ios

RMCircle doesn't appear to be defined in swift, how can I draw a simple circle?

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

instead I only have MLGShape and MLGPolygon but no MLGCircle

like image 999
abc123 Avatar asked Feb 10 '23 14:02

abc123


1 Answers

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

Rewrote the solution to 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)
}
like image 127
Lengo Avatar answered Feb 23 '23 07:02

Lengo