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

Swe*_*per 7 google-maps uipopovercontroller ios swift

当用户点击Google地图上的某个标记时,我想将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
Run Code Online (Sandbox Code Playgroud)

当我尝试设置时,问题就出现sourceViewUIPopoverPresentationController.我以为使用该iconView属性会起作用,但没有.始终存在错误,sourceView表示未设置.

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

PS这是标记的创建方式:

func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
    let marker = GMSMarker(position: coordinate)
    marker.map = mapView
}
Run Code Online (Sandbox Code Playgroud)

Sah*_*nda 4

输出: 在此输入图像描述

代码:

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
  }
  
}
Run Code Online (Sandbox Code Playgroud)

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