如何获取Google Map标记的UIView实例?

时间:2016-09-09 16:34:35

标签: ios swift google-maps uipopovercontroller

当用户点击谷歌地图上的其中一个标记时,我想将VC显示为弹出窗口。

我想这样做的原因是因为我想控制点击标记时弹出的视图。我尝试使用mapView(mapView: GMSMapView, markerInfoWindow marker: GMSMarker)委托方法。但我不知道如何在该方法中创建一个控制标记信息窗口视图的视图控制器。

要将VC呈现为弹出窗口,我这样做了:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MarkerInfoController")
vc.modalInPopover = true
vc.modalPresentationStyle = .Popover
print(marker.iconView)
vc.popoverPresentationController!.sourceView = marker.iconView

self.presentVC(vc) // this is from EZSwiftExtensions. Don't worry about it

当我尝试设置sourceView的{​​{1}}时出现问题。我认为使用UIPopoverPresentationController属性可行,但不行。始终存在错误,指出未设置iconView

如何获取标记的sourceView个实例,以便我可以将其分配给UIView

P.S。这就是标记的创建方式:

sourceView

3 个答案:

答案 0 :(得分:4)

输出: enter image description here

代码:

import UIKit
import GoogleMaps

class MapViewController: UIViewController {

  @IBOutlet weak var mapView: GMSMapView!
  var sourceView: UIView?
}

extension MapViewController: GMSMapViewDelegate {

  func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    mapCenterPinImage.fadeOut(0.25)

    // Adding a delay becuase when click on marker Camera changes it position
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {

      let location = marker.accessibilityActivationPoint
      self.sourceView = UIView(frame: CGRect(x: location.x, y: location.y, width: 1, height: 1))
      self.view.addSubview(self.sourceView!)

      let popController = MyPopUpViewController()
      popController.modalPresentationStyle = UIModalPresentationStyle.popover
      popController.preferredContentSize = CGSize(width: 200, height: 200)
      popController.popoverPresentationController?.delegate = self
      popController.popoverPresentationController?.sourceView = self.sourceView
      self.present(popController, animated: true)
    }

    return false
  }

}
extension MapViewController: UIPopoverPresentationControllerDelegate{
  func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
  }
  func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
    sourceView?.removeFromSuperview()
    sourceView = nil
    return true
  }

}

我所做的基本上创建了一个UIView,并在运行时将其添加到ViewController,并将其设置为Popup的源,并在关闭时将其设置为nil。 根据设备屏幕, marker.accessibilityActivationPoint 是X和Y的来源

答案 1 :(得分:0)

您可以仅自定义GMS标记的infoView,而无需显示viewControlle并在那里执行所需的操作。您可以在这里查看这篇文章。我遇到了与您的问题类似的事情,这对我有所帮助。希望它也对您有所帮助。 :D

https://medium.com/@matschmidy/how-to-implement-custom-and-dynamic-map-marker-info-windows-for-google-maps-ios-e9d993ef46d4

答案 2 :(得分:0)

您可以在下面执行的操作是gmsMapView委托方法

 func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool 

在这里您将获得点击的标记,对于源视图,您可以访问

 marker.iconView 

我认为它将解决您的问题,希望对您有所帮助:)