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

前端 未结 8 2028
死守一世寂寞
死守一世寂寞 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:19

    Swift 3/ Xcode 8 here:

    func addRadiusCircle(location: CLLocation){
        if let poll = self.selectedPoll {
            self.mapView.delegate = self
            let circle = MKCircle(center: location.coordinate, radius: 10)
            self.mapView.add(circle)
        }
    }
    
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if overlay is MKCircle {
            let circle = MKCircleRenderer(overlay: overlay)
            circle.strokeColor = UIColor.red
            circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
            circle.lineWidth = 1
            return circle
        } else {
            return MKPolylineRenderer()
        }
    }
    

    Then call like so:

    self.addRadiusCircle(location: CLLocation(latitude: YOUR_LAT_HERE, longitude: YOUR_LNG_HERE))
    

提交回复
热议问题