Kasey
Kasey

Reputation: 414

How to add circle around map pin in Swift?

I've been trying to figure this one out with no luck.

What I can do:

What I can't figure out:

How do I create a geofenced circle around this location?

 func setupData() {
    // 1. check if system can monitor regions
    if CLLocationManager.isMonitoringAvailable(for: 
 CLCircularRegion.self) {

        // 2. region data
        let title = "Marina Bar Hop"
        let coordinate = CLLocationCoordinate2DMake(33.97823607957177, -118.43823725357653)
        let regionRadius = 300.0

        // 3. setup region
        let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude,
            longitude: coordinate.longitude), radius: regionRadius, identifier: title)
        locationManager.startMonitoring(for: region)

        // 4. setup annotation
        let restaurantAnnotation = MKPointAnnotation()
        restaurantAnnotation.coordinate = coordinate;
        restaurantAnnotation.title = "\(title)";
        mapView.addAnnotation(restaurantAnnotation)

        // 5. setup circle
        let circle = MKCircle(center: coordinate, radius: regionRadius)
        mapView.addOverlay(circle)
    }
    else {
        print("System can't track regions")
    }
}

// 6. draw circle
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let circleRenderer = MKCircleRenderer(overlay: overlay)
    circleRenderer.strokeColor = UIColor.red
    circleRenderer.lineWidth = 1.0
    return circleRenderer
} 

See "draw circle" and "set up circle" ^

In case there's an error I'm not seeing, the rest of the code is here.

Any help is greatly appreciated! I thought I've done everything to solve this issue, but it still won't work. :( Thank you in advance!

Upvotes: 3

Views: 3084

Answers (1)

matt
matt

Reputation: 535138

The trouble with this question is that the code in the question does not represent faithfully the real code. Fortunately, you also posted the real code:

https://github.com/kcapretta/RadiusSocialNetworkingApp/blob/master/RadiusMap/RadiusLocationViewController

In that version of the code, we see clearly that your delegate method

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay)
     -> MKOverlayRenderer {

is buried inside your viewDidLoad. Thus it is purely a local function, invisible to all other code. You need to get it out of there so that it is a method of the view controller.

To demonstrate, here's a slight variant on your code, leaving out the unnecessary stuff about the region monitoring and the user's current location and concentrating just on the annotation and the circle overlay:

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet var mapView : MKMapView!
    let coordinate = CLLocationCoordinate2DMake(33.97823607957177, -118.43823725357653)
    // this is a _method_
    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.delegate = self

        mapView.region = MKCoordinateRegion(center: coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)

        let title = "Marina Bar Hop"
        let restaurantAnnotation = MKPointAnnotation()
        restaurantAnnotation.coordinate = coordinate
        restaurantAnnotation.title = title
        mapView.addAnnotation(restaurantAnnotation)

        let regionRadius = 300.0
        let circle = MKCircle(center: coordinate, radius: regionRadius)
        mapView.addOverlay(circle)
    }
    // this is a _method_
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let circleRenderer = MKCircleRenderer(overlay: overlay)
        circleRenderer.strokeColor = UIColor.red
        circleRenderer.lineWidth = 1.0
        return circleRenderer
    }
}


The result:

enter image description here

Upvotes: 5

Related Questions