我在Google地图上显示了标记,并希望添加每个位置名称的UILabel。

看到GMSMarker属性标题特定于标记的infoWindow,并且从现有研究的细微选择中,可能必须制作一个自定义图标?

是否有iOS实例(更容易实现)的示例?

最佳答案

为了创建自定义标记,您必须将GMSMarker子类化,并根据需要创建它的iconView

Google Maps documentation:


  • (UIView*) iconView [read, write, assign]


  • 要渲染的标记视图。如果为零,则退回到icon属性
    代替。

    例:
    class CustomMarker: GMSMarker {
    
        var label: UILabel!
    
        init(labelText: String) {
            super.init()
    
            let iconView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 50, height:     80)))
            iconView.backgroundColor = .white
    
            label = UILabel(frame: CGRect(origin: .zero, size: CGSize(width:     iconView.bounds.width, height: 40)))
            label.text = labelText
            iconView.addSubview(label)
    
            self.iconView = iconView
        }
    }
    

    用法:
    let marker = CustomMarker(labelText: "my_label")
    marker.position = CLLocationCoordinate2D(latitude: 0, longitude: 0)
    marker.map = //your mapView object
    

    请注意,iconView仅用于为要渲染的标记创建减价。您的标记不会像通常的UIView那样难处理,因此向其添加任何手势或复杂的可滚动视图都不会产生任何效果。

    关于ios - 在iOS中将地名标签添加到Google Map Markers,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50287770/

    10-14 22:05
    查看更多