mapkitview not showing circle object overlay in Swift

82 Views Asked by At

My objective is to show a blue or yellow colored circle of a given radius so that later on I can search for cafes and pin them within that location.

I have this code so far and I have done everything I could! There is no error and the main location gets easily pinned and the annotation shows as well but just the circle doesn't appear.

//  ViewController2.swift

import UIKit
import MapKit

class ViewController2: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate

{
    @IBOutlet weak var mapView: MKMapView!
    @IBAction func search(_ sender: Any)
    {
        //get location
        var dec = location1.text
    }

    @IBOutlet weak var location1: UITextField!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label11: UILabel!
    @IBOutlet weak var baclbtn: UIButton!
    @IBOutlet weak var map1: MKMapView!

    var gllat:Double=0
    var gllong:Double=0
    var loc:String=""

    override func viewDidLoad()
    { super.viewDidLoad()
       map1.delegate=self

       //pin location on required area
        let anotaion = MKPointAnnotation()
        anotaion.coordinate=CLLocationCoordinate2DMake(gllat, gllong)
       self.mapView.addAnnotation(anotaion)

        //zoom in to the required location
        let coordinate:CLLocationCoordinate2D=CLLocationCoordinate2DMake(gllat,gllong)
        let span1 = MKCoordinateSpan(latitudeDelta:0.1,longitudeDelta:0.1)
        let region = MKCoordinateRegion(center:coordinate,span:span1)

        self.mapView.setRegion(region, animated: true)

        //add the circle object overlay
        let _coordinate:CLLocationCoordinate2D=CLLocationCoordinate2DMake(gllat, gllong)
        let radius1=CLLocationDistance(bitPattern: 1000)
        let cl = MKCircle(center: _coordinate, radius: radius1)

        self.mapView.addOverlay(cl)

    }

        func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
    {
            guard let circelOverLay = overlay as? MKCircle else {return MKOverlayRenderer()}

            let circleRenderer = MKCircleRenderer(circle: circelOverLay)
            circleRenderer.fillColor = .blue
            //circleRenderer.alpha = 0.2
            return circleRenderer
    }
}
0

There are 0 best solutions below