You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift 5中Google Map标记标题字号设置及标题遮挡图标问题求助

我太懂你这个烦恼了——自定义的标记图标老是被标题挡住,哪怕图标移动、旋转的时候这个问题还跟着来。这在Google Maps iOS SDK里是个挺常见的布局调整问题,给你几个靠谱的解决办法:

方法1:调整标记的锚点(最快捷)

Google Maps标记的默认锚点是(0.5, 0.5)(也就是图标的中心位置),而标题默认显示在锚点的下方,如果你图标比较大,就很容易和标题重叠。咱们把锚点改成图标的底部中心,这样标题就会乖乖显示在图标正下方,不会挡住图标了:

let marker = GMSMarker()
marker.title = "D M Th 13-6573"
marker.icon = UIImage(named: "item_car_slow")
// 设置锚点为图标底部中心(x:0.5是水平居中,y:1.0是底部)
marker.groundAnchor = CGPoint(x: 0.5, y: 1.0)
marker.rotation = CLLocationDegrees(heading)
marker.map = self.googleMap
self.googleMap.selectedMarker = marker

方法2:自定义标记视图(最灵活)

如果锚点调整还满足不了你的需求,比如你想给标题换个样式、调整和图标的间距,那就直接自定义整个标记视图,完全掌控布局:

// 创建一个自定义容器视图,尺寸根据你的图标和标题来设
let customMarkerView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 50))

// 添加图标ImageView
let iconImageView = UIImageView(image: UIImage(named: "item_car_slow"))
iconImageView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
iconImageView.contentMode = .scaleAspectFit
customMarkerView.addSubview(iconImageView)

// 添加标题Label,放在图标下方,间距可以自己调
let titleLabel = UILabel(frame: CGRect(x: 0, y: 42, width: 40, height: 8))
titleLabel.text = "D M Th 13-6573"
titleLabel.font = UIFont.systemFont(ofSize: 8)
titleLabel.textAlignment = .center
titleLabel.textColor = .black
customMarkerView.addSubview(titleLabel)

// 把自定义视图赋值给marker的iconView
let marker = GMSMarker()
marker.iconView = customMarkerView
// 锚点还是设为容器视图的底部中心,确保标记定位准确
marker.groundAnchor = CGPoint(x: 0.5, y: 1.0)
marker.rotation = CLLocationDegrees(heading)
marker.map = self.googleMap
self.googleMap.selectedMarker = marker

方法3:调整标题偏移量(临时快速修复)

如果只是想快速把标题挪开,不用改锚点或自定义视图,可以直接调整标题相对于标记的偏移量,把标题往下拉一点:

let marker = GMSMarker()
marker.title = "D M Th 13-6573"
marker.icon = UIImage(named: "item_car_slow")
// 把标题向下偏移20个点,数值可以根据你的图标大小调整
marker.titleOffset = CGPoint(x: 0, y: 20)
marker.rotation = CLLocationDegrees(heading)
marker.map = self.googleMap
self.googleMap.selectedMarker = marker

选择建议

  • 只是单纯位置重叠:优先用方法1,简单高效
  • 需要定制标题样式/更灵活的布局:选方法2,完全可控
  • 临时快速解决小问题:用方法3,成本最低

内容的提问来源于stack exchange,提问作者Enamul Haque

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 10:12:38