自定义infoView用于标记数组快速

时间:2019-05-31 22:39:40

标签: swift google-maps

我有一个使用Googlemaps的应用程序,我能够为所有所需的坐标创建标记,但是现在面临的问题是我想在用户点击标记以显示自定义UIView时显示与该标记关联的信息

func showFeaturedProperties(properties: [FeaturedProperties]) {
        self.properties = properties
        properties.forEach { (props) in
            var lat = Double(props.latitude!) as! Double
            var lng = Double(props.longitude!) as! Double


            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2D(latitude: lat,
                                                     longitude: lng)
            marker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
            marker.isFlat = true
            marker.map = mapView
            if props.typeId == 1 {
                marker.icon = #imageLiteral(resourceName: "_map_rent_icon")
            } else {
                marker.icon = #imageLiteral(resourceName: "_map_buy_icon")
            }

            marker.setIconSize(scaledToSize: .init(width: 25, height: 25))
            marker.iconView?.contentMode = .scaleAspectFit
            self.markers.append(marker)
        }

    }

上面的代码在地图上显示了标记,单击标记后如何显示详细信息?

到目前为止,我已经扩展了GMSMapViewDelegate

func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMarker) -> UIView? {

        if let index = markers.index(of: marker) {
            let tappedState = properties[index]
            log(tappedState.id)
        }

        let infoView = BaseCardView()

        return infoView
    }

这仍然无法正常工作,欢迎任何帮助

1 个答案:

答案 0 :(得分:0)

  1. 使用xib文件创建信息窗口标记视图

    import UIKit
    import GoogleMaps
    class InfoMarkerView: UIView {
    
    var view:UIView!
    var parentVC:StartRideViewController?
    
    override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
    }
    
    required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    setup()
    }
    
    func setup() {
    view = loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = UIViewAutoresizing.flexibleWidth
    view.autoresizingMask = UIViewAutoresizing.flexibleHeight
    addSubview(view)
    }
    
    func loadViewFromNib() -> UIView {
    let bundle = Bundle(for:type(of: self))
    let nib = UINib(nibName: "InfoMarkerView", bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
    return view
    }
    
    func UpdateView(marker:GMSMarker){
    // set values to labels here
    }
    }
    
  2. 将GMSMapViewDelegate添加到您的viewcontroller文件中

    var mapView: GMSMapView!
    var infoMarkerView:InfoMarkerView?
    
     if(latitude != 0 && longitude != 0){
                    let position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
                    let marker = GMSMarker(position: position)
    
                    var myData = Dictionary<String, Any>()
                    myData["title"] = ridername
                    myData["snippet"] = ridertime
                    myData["photoUrl"] = photoUrl
                    myData["position"] = position
                    myData["accuracy"] = accuracy
                    myData["uid"] = riderUID
                    myData["status"] = riderStatus
                    marker.userData = myData
    
    
                    marker.title = ridername
                    marker.snippet = ridertime
                    marker.position = position
                    marker.iconView = self.showImageonMap(imgPath:photoUrl)
    
                    marker.map = self.mapView
    
    
                }
    
    // MARK: GMSMapViewDelegate method implementation
    
    func mapView(_ mapView: GMSMapView!, didTapAt coordinate: CLLocationCoordinate2D) {
    print("here")
    if infoMarkerView != nil {
        infoMarkerView?.removeFromSuperview()
    }
    }
    
    func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay)
    {
    print(overlay.zIndex)
    print("User Tapped Layer: \(overlay)")
    if infoMarkerView != nil {
        infoMarkerView?.removeFromSuperview()
    }
    }
    
    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool     {
    //let update = GMSCameraUpdate.zoom(by: zoomLevel)
    //mapView.animate(with: update)
    
    if infoMarkerView != nil {
        infoMarkerView?.removeFromSuperview()
    }
    infoMarkerView = InfoMarkerView(frame: CGRect(x: marker.infoWindowAnchor.x, y: marker.infoWindowAnchor.y, width:180, height: 120))
    infoMarkerView?.parentVC = self
    // Offset the info window to be directly above the tapped marker
    infoMarkerView?.center = mapView.projection.point(for: marker.position)
    infoMarkerView?.center.y = (infoMarkerView?.center.y)! - 125
    infoMarkerView?.UpdateView(marker: marker)
    
    mapView.addSubview(infoMarkerView!)
    return true
    }
    
    func mapView(_ mapView: GMSMapView, didBeginDragging marker: GMSMarker) {
    if infoMarkerView != nil {
        infoMarkerView?.removeFromSuperview()
    }
    }
    
    func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    if infoMarkerView != nil {
        infoMarkerView?.removeFromSuperview()
    }
    }