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
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With