我有一张里面有地图的视图。我想实现以下目标:当用户点击地图时,会显示并选择注释(因此会显示标注气泡),直到用户点击地图上的另一个点(在这种情况下,应删除之前的注释) )。
问题是,当我点击地图时,注释会被添加并选择一段时间,然后以某种方式取消选择(因此标注消失)。我希望标注气泡持续到用户点击另一个点为止
在下面有我的尝试,有人可以帮助我吗?
@objc func handleTap(_ sender: UITapGestureRecognizer){
//...some code that computes the country code starting from latitude and longitude
var countryCurrency = ""
func countryRequest(countryLabel: String, completion: @escaping (String)->()){
//api request that receives the currency name of the country
completion(countryCurrency)
}
countryRequest(countryLabel: countryLabel){ countryCurrency in
DispatchQueue.main.async {
let locat:CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationCoord.latitude, locationCoord.longitude)
let annotation = MKPointAnnotation()
annotation.coordinate = locat
annotation.title = countryCurrency
annotation.subtitle = "subtitle"
self.mapView.addAnnotation(annotation)
self.mapView.selectAnnotation(annotation, animated: true)
}
}
}
我还有MKMapViewDelegate的扩展来自定义标注:
extension MapsItem: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is MKPointAnnotation){
return nil
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "demo")
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "demo")
annotationView!.canShowCallout = true
annotationView!.sizeToFit()
}
else{
annotationView!.annotation = annotation
}
return annotationView
}
}
提前致谢!!
试试这个
在添加注释之前将isTappingNow设置为true,并在选择之后将其设置为false
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView)
{
if(!isTappingNow)
{
self.mapView.selectAnnotation(view.annotation, animated: false)
}
}