当用户点击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
当我尝试设置sourceView
的UIPopoverPresentationController
时出现问题。我认为使用iconView
属性会起作用,但没有。总是有一个错误说没有设置sourceView
。
如何获得标记的UIView
实例,以便我可以将其分配给sourceView
?
附:这是标记的创建方式:
func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
let marker = GMSMarker(position: coordinate)
marker.map = mapView
}
码:
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的来源
您可以自定义GMS标记的infoView,而不是显示viewControlle并在那里执行所需的操作。你可以在这看一下这篇文章。我遇到了类似于你的问题,这帮助了我。希望它也可以帮到你。 :d
你可以在下面做一个gmsMapViewdelegate方法
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
在这里,您将获得您点击的标记,并且您可以访问源视图
marker.iconView
我认为这将解决你的问题,希望它有帮助:)