Draw a circle of 1000m radius around users location in MKMapView

前端 未结 8 2042
死守一世寂寞
死守一世寂寞 2020-11-29 17:18

(Using iOS 5 and Xcode 4.2)

I have an MKMapView and want to draw a circle of 1000m radius around the user location.

On the surface it would seem tha

8条回答
  •  伪装坚强ぢ
    2020-11-29 18:07

    An updated version for iOS 8.0 using Swift.

    import Foundation
    import MapKit
    
    class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
        var locationManager: CLLocationManager = CLLocationManager()
    
        @IBOutlet var mapView: MKMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // We use a predefined location
            var location = CLLocation(latitude: 46.7667 as CLLocationDegrees, longitude: 23.58 as CLLocationDegrees)
    
            addRadiusCircle(location)
        }
    
        func addRadiusCircle(location: CLLocation){
            self.mapView.delegate = self
            var circle = MKCircle(centerCoordinate: location.coordinate, radius: 10000 as CLLocationDistance)
            self.mapView.addOverlay(circle)
        }
    
        func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
            if overlay is MKCircle {
                var circle = MKCircleRenderer(overlay: overlay)
                circle.strokeColor = UIColor.redColor()
                circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
                circle.lineWidth = 1
                return circle
            } else {
                return nil
            }
        }
    }
    

提交回复
热议问题