(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
It's easy to add a circle. Conform to MKMapViewDelegate. follow the bellow steps,,,
Step 1 :
CLLocationCoordinate2D center= {self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude};
// Add an overlay
MKCircle *circle= [MKCircle circleWithCenterCoordinate:center radius: 20000];//your distance like 20000(like meters)
[myMapView addOverlay:circle];
Step 2 :
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKCircleView *C_View = [[MKCircleView alloc] initWithOverlay:overlay];
[C_View setFillColor:[UIColor lightGrayColor]];
[C_View setStrokeColor:[UIColor blackColor]];
[C_View setAlpha:0.5f];
return C_View;
}
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))