在Google Maps Swift中删除自定义信息窗口后如何取消选择GMSMarker [英] How to deselect a GMSMarker after removing custom infowindow in google maps swift

查看:75
本文介绍了在Google Maps Swift中删除自定义信息窗口后如何取消选择GMSMarker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在使用Google Maps iOS SDK 4.0.0,我的要求是这样的,当我点击一个标记时,它应该添加我很容易实现的UIViewContoller视图.看一下以下代码:

So I have been using Google Maps iOS SDK 4.0.0 and my requirement is like this when I tap on a marker it should add UIViewContoller's view which I easily achieved. Take a look on the following code:

var customeVC:CustomViewController?

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    
    customeVC = CustomViewController(nibName: "CustomViewController", bundle: nil)
    customeVC?.delegate = self
    self.addChild(customeVC!)
    customeVC?.view.frame = self.view.frame
    self.view.addSubview(customeVC!.view)
    customeVC?.didMove(toParent: self)

    // Remember to return false
    // so marker event is still handled by delegate
    return false
}


func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
    
    //Empty the default infowindow
    return UIView()
}


extension MapViewController: CustomViewControllerDelegate {
    // Triggers when I close the full screen view of CustomViewController
    func didCloseWindow() {
        customeVC?.willMove(toParent: nil)
        customeVC?.removeFromParent()
        customeVC?.view.removeFromSuperview()
        customeVC = nil
    }
}

现在主要的问题是,如果我再次单击同一标记(第二次),则在关闭窗口/视图后,它不显示该视图.但是,如果我再次单击(第三次),它将显示.

Now the main the problem is, after closing the window/view if I click on the same marker again (2nd time) its doesn't show the view. But if I click once again (3rd time), it shows.

因此,我怀疑在删除视图之后,不会取消选择标记.但是当我第二次点击时,它会被取消选择,而第三次点击时,它会再次被选择.

So I'm suspecting after removing the view the marker doesn't get deselected. But when I tap for the 2nd time its gets deselected and tap for the 3rd time get selected again.

我有文本字段&CustomViewController中的按钮,这就是为什么我没有在委托函数 mapView(_ mapView:GMSMapView,markerInfoWindow marker:GMSMarker)-中添加此视图的原因UIView?.基本上,我遵循了文章,其中可让您在InfoWindow中单击.

I have textfields & buttons inside CustomViewController thats why I didn't add this view inside the delegate function mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView?. Basically I followed this article which lets you click inside InfoWindow.

在删除视图时,我还尝试在 didTap 委托方法内使用 mapView.selectedMarker = marker mapView.selectedMarker = nil .

I also tried to mapView.selectedMarker = marker inside didTap delegate method and mapView.selectedMarker = nil when removing the view.

如何取消选择标记,以便每次单击同一标记时它都应显示视图?

How do I deselect the marker so that each time I click on the same marker its should show the view?

任何帮助将不胜感激.预先感谢.

Any help will be appreciated. Thanks in advance.

推荐答案

尝试了几天后终于解决了.基本上,每次仅在绘制标记时,才可以点击标记.但是,如果您在叠加层或多边形内绘制标记,则第二次点击同一标记不起作用,因为第二次触发了以下 GMSMapViewDelegate 方法.

After trying for several days finally I solved it. Basically tapping on marker works every time only if you draw only markers. But if you draw markers inside an overlay or a polygon then tapping on same marker for the 2nd time didn't work because 2nd time the following method of GMSMapViewDelegate gets fired.

func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay)

我没有提到,我在覆盖层中绘制标记是因为我从没想到它会引起点击问题.我发现其他人也面临

I didn't mention that, I was drawing markers inside an overlay because I never thought it can cause tapping issue. I found someone else also faced the issue earlier but his question was unanswered. So here is what I needed to update inside that delegate method to make it work and the rest code are the same.

当我点击某个多边形时,我将 isTappable 多边形属性设置为 false ,然后我就可以玩里面绘制的标记了.同样,一旦用户单击其他多边形,我就会将先前选择的多边形的 isTappable 属性重置为 true .

I'm setting the isTappable polygon property to false when I'm tapping on a certain polygon and then I can play around with the markers which are drawn inside. Also I'm resetting the isTappable property to true of previously selected polygon as soon as user clicks on other polygon.

class ViewController: GMSMapViewDelegate {
    @IBOutlet weak var mapView:GMSMapView!
    private var selectedPolygon: GMSAptivePolygon?
    
    func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
        
        if let polygon = overlay as? GMSPolygon {
            
            // Make sure to restrict markers for a polygon if tapped more than once
            if selectedPolygon != polygon {
                
                // Reset polygon to tappable
                selectedPolygon?.isTappable = true
                
                // Fetch all annotations of this polygon if exits
                fetchMarkers(for: polygon)
                
                polygon.isTappable = false
                selectedPolygon = polygon
            }
        }
    }
}

这篇关于在Google Maps Swift中删除自定义信息窗口后如何取消选择GMSMarker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆