Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. func polygonCircleWhatHere(coordinate: CLLocationCoordinate2D, withMeterRadius: Double) {
  2. let degreesBetweenPoints = 8.0
  3. //45 sides
  4. let numberOfPoints = floor(360.0 / degreesBetweenPoints)
  5. let distRadians: Double = withMeterRadius / 6371000.0
  6. // earth radius in meters
  7. let centerLatRadians: Double = coordinate.latitude * Double.pi / 180
  8. let centerLonRadians: Double = coordinate.longitude * Double.pi / 180
  9. var coordinates = [CLLocationCoordinate2D]()
  10. //array to hold all the points
  11. for index in 0 ..< Int(numberOfPoints) {
  12. let degrees: Double = Double(index) * Double(degreesBetweenPoints)
  13. let degreeRadians: Double = degrees * Double.pi / 180
  14. let pointLatRadians: Double = asin(sin(centerLatRadians) * cos(distRadians) + cos(centerLatRadians) * sin(distRadians) * cos(degreeRadians))
  15. let pointLonRadians: Double = centerLonRadians + atan2(sin(degreeRadians) * sin(distRadians) * cos(centerLatRadians), cos(distRadians) - sin(centerLatRadians) * sin(pointLatRadians))
  16. let pointLat: Double = pointLatRadians * 180 / Double.pi
  17. let pointLon: Double = pointLonRadians * 180 / Double.pi
  18. let point: CLLocationCoordinate2D = CLLocationCoordinate2DMake(pointLat, pointLon)
  19. coordinates.append(point)
  20. }
  21. let polygon = MGLPolygon(coordinates: &coordinates, count: UInt(coordinates.count))
  22.  
  23. draggableAnnotationPoint.coordinate = CLLocationCoordinate2D(latitude: coordinates[0].latitude, longitude: coordinates[0].longitude)
  24.  
  25. let centerAnnotationPoint = MGLPointAnnotation()
  26. centerAnnotationPoint.coordinate = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
  27. self.mapView.addAnnotations([polygon,centerAnnotationPoint,draggableAnnotationPoint])
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement