Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a circle with a certain radius to my MKpointAnnotation IOS Swift

Tags:

ios

swift

mapkit

Could someone point me in the right direction for adding a radius to my point. Its pretty standard code, I've tried adding a MKcircle but that hasn't anything to my map.

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var maps: MKMapView!
var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()




    var latitude:CLLocationDegrees = 54.53
    var longitude:CLLocationDegrees = -6.055
    var latDelta:CLLocationDegrees = 0.015 //difference of lats from one side of screen to another
    var longDelta:CLLocationDegrees = 0.015 //difference of lats from one side of screen to another

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)

    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude,longitude)

    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    var lenght:CLLocationDistance = 2


    var point = MKPointAnnotation()
    self.maps.delegate = self
    point.title = "Home"
    point.subtitle = "time for home"
    point.coordinate = location



    maps.addAnnotation(point)
    maps.selectAnnotation(point, animated: true)
    var cir:MKCircle = MKCircle(centerCoordinate: location, radius: length) //added this but nothing is displayed on map

    maps.addOverlay(cir)



    maps.setRegion(MKCoordinateRegionMake(point.coordinate, MKCoordinateSpanMake(latDelta,longDelta)), animated: true)
    // Do any additional setup after loading the view, typically from a nib.


}



func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if (annotation.isKindOfClass(MKUserLocation)){
        return nil
    }
    var myPin = mapView.dequeueReusableAnnotationViewWithIdentifier("MyIdentifier") as? MKPinAnnotationView
    if myPin != nil {
        return myPin
    }

    myPin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "MyIdentifier")
    myPin?.pinColor = .Green
    return myPin
}
like image 653
Jack Avatar asked Dec 28 '25 02:12

Jack


1 Answers

Implement rendererForOverlay delegate:

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    var overlayRenderer : MKCircleRenderer = MKCircleRenderer(overlay: overlay);
    overlayRenderer.lineWidth = 1.0
    overlayRenderer.strokeColor = UIColor.redColor()
    return overlayRenderer
}

enter image description here

like image 184
Kosuke Ogawa Avatar answered Dec 30 '25 17:12

Kosuke Ogawa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!