我的目标是显示给定半径的蓝色或黄色圆圈,以便以后可以搜索咖啡馆并将其固定在该位置。
到目前为止,我已经有了这段代码,我已经尽力了!没有错误,主要位置容易固定,注释也显示出来,但没有出现圆圈。
// 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
}
}