首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于标记数组的自定义infoView

用于标记数组的自定义infoView
EN

Stack Overflow用户
提问于 2019-05-31 22:39:39
回答 1查看 858关注 0票数 0

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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
    }

这是行不通的,但欢迎任何帮助。

EN

回答 1

Stack Overflow用户

发布于 2019-06-01 05:02:15

  1. 使用xib文件创建infowindow标记视图 导入UIKit导入GoogleMaps类InfoMarkerView: UIView { var视图:UIView!var :StartRideViewController?重写init(框架: CGRect) {super.init(框架:框架)设置()}必需的init(编码器aDecoder: NSCoder) {super.init(编码器: aDecoder)!setup() } func安装程序(){ view = loadViewFromNib() 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) as!UIView返回视图} func UpdateView(标记:GMSMarker){ //将值设置到这里的标签}}
  2. 将GMSMapViewDelegate添加到视图控制器文件中 mapView: GMSMapView!var infoMarkerView:InfoMarkerView?如果(纬度=0 &&经度!= 0){让位置=CLLocationCoordinate2D(纬度:纬度,经度:经度),让标记=GMSMarker(位置:位置) var myData = Dictionary() myData"title“= ridername myData"snippet”= ridertime myData" photoUrl“=photoUrl myData”photoUrl“位置”= position myData“精度”=精确度myData“= riderUID myData"status”= riderStatus marker.userData =myDatamarker.title = ridername marker.snippet = ridertime marker.position = marker.iconView = self.showImageonMap(imgPath:photoUrl) marker.map = self.mapView } // MARK: GMSMapViewDelegate方法实现func mapView(_ mapView: GMSMapView!)didTapAt坐标: CLLocationCoordinate2D) { infoMarkerView != nil { infoMarkerView?.removeFromSuperview() } func mapView(_ mapView: GMSMapView,didTap覆盖: GMSOverlay) { print(overlay.zIndex) print(“用户点击层:(覆盖)”)如果infoMarkerView != nil { infoMarkerView?.removeFromSuperview() } func mapView(_ mapView: GMSMapView,didTap标记: GMSMarker) -> Bool { //let update = GMSCameraUpdate.zoom(by: zoomLevel) //mapView.动画(带: update)如果infoMarkerView != nil { infoMarkerView?.removeFromSuperview() } infoMarkerView =InfoMarkerView(框架: CGRect(x: marker.infoWindow.x,y: marker.infoWindow.y,宽度:180,(高度:120)( infoMarkerView?.parentVC = self //偏移信息窗口,使其直接位于标记infoMarkerView?.center = mapView.projection.point(for: marker.position) infoMarkerView?.cent.y=(infoMarkerView?.cent.y)!- 125 infoMarkerView?.UpdateView(标记:标记) mapView.addSubview(infoMarkerView!)返回true } func mapView(_ mapView: GMSMapView,didBeginDragging标记: GMSMarker) { if infoMarkerView != nil { infoMarkerView?.removeFromSuperview() } func mapView(_ mapView: GMSMapView,didChange位置: GMSCameraPosition) { if infoMarkerView = nil { infoMarkerView?.removeFromSuperview() }}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56402421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档