如何防止自动取消选择注释 swift
how to prevent deselection of an annotation automatically swift
我有一个视图,里面有一张地图。我想实现以下目标:当用户点击地图时显示并选择注释(因此显示标注气泡)直到用户点击地图上的另一个点(在这种情况下,应该删除之前的注释).
问题是,当我点击地图时,注释被添加并被选中了一小会儿,然后它以某种方式被取消选择(因此标注消失)。我希望标注气泡持续存在,直到用户点击另一个点
下面是我的尝试,请问有没有大佬帮帮我?
@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)
}
}
一直在寻找解决方案,不知道为什么会这样,是不是bug?到现在为止,我做了一个技巧来延迟它出现。 @Sh_Khan的方案会再闪一次callout,我的方案会让用户等一会
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
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)
}
我有一个视图,里面有一张地图。我想实现以下目标:当用户点击地图时显示并选择注释(因此显示标注气泡)直到用户点击地图上的另一个点(在这种情况下,应该删除之前的注释).
问题是,当我点击地图时,注释被添加并被选中了一小会儿,然后它以某种方式被取消选择(因此标注消失)。我希望标注气泡持续存在,直到用户点击另一个点
下面是我的尝试,请问有没有大佬帮帮我?
@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)
}
}
一直在寻找解决方案,不知道为什么会这样,是不是bug?到现在为止,我做了一个技巧来延迟它出现。 @Sh_Khan的方案会再闪一次callout,我的方案会让用户等一会
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
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)
}