- 是否有官方方法可以在 iOS 版 Google 地图(GMSMapView)中设置自定义用户位置点?
- 有没有一种已知的方法来“破解”它?喜欢遍历所有子视图和图层并找出蓝点?
- 即使你不能自定义它的外观,你能控制它的 z 顺序索引吗?当您有许多标记时,小蓝点会被隐藏,有时您希望它始终可见。
谢谢
谢谢
您可以尝试在以下位置找到图像:
GoogleMaps.framework > 资源 > GoogleMaps.bundle 或 GoogleMaps.framework > 资源 > GoogleMaps.bundle > GMSCoreResources.bundle
我对这些文件进行了快速搜索,发现与该蓝点相关的唯一文件是 GMSSprites-0-1x。
请阅读谷歌地图的条款和条件,因为这可能不合法。
您可以将地图设置myLocationEnabled
为NO
. 这将隐藏默认位置点。然后使用一个实例CLLocationManager
来给你你的位置。在CLLocationManager
didUpdateLocations
方法内部,您可以设置自定义GMSMarker
. 将其图标属性设置为您希望使用[UIImage imageNamed:]
. 这将使您达到预期的效果。
斯威夫特 4
禁用默认的谷歌地图当前位置标记(默认禁用):
mapView.isMyLocationEnabled = false
创建一个标记作为视图控制器的实例属性(因为委托需要访问它):
let currentLocationMarker = GMSMarker()
初始GMSMarker
值设定项允许 aUIImage
或 aUIView
作为自定义图形,而不是 a UIImageView
。如果您想对图形进行更多控制,请使用UIView
. 在您的loadView
或viewDidLoad
(无论您在哪里配置地图)中,配置标记并将其添加到地图中:
// configure custom view
let currentLocationMarkerView = UIView()
currentLocationMarkerView.frame.size = CGSize(width: 40, height: 40)
currentLocationMarkerView.layer.cornerRadius = 40 / 4
currentLocationMarkerView.clipsToBounds = true
let currentLocationMarkerImageView = UIImageView(frame: currentLocationMarkerView.bounds)
currentLocationMarkerImageView.contentMode = .scaleAspectFill
currentLocationMarkerImageView.image = UIImage(named: "masterAvatar")
currentLocationMarkerView.addSubview(currentLocationMarkerImageView)
// add custom view to marker
currentLocationMarker.iconView = currentLocationMarkerView
// add marker to map
currentLocationMarker.map = mapView
剩下的就是给标记一个坐标(最初和每次用户的位置改变时),你可以通过CLLocationManagerDelegate
委托来完成。
extension MapViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!
// update current location marker
currentLocationMarker.position = CLLocationCoordinate2D(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)
}
}
位置管理器生成的前几个位置可能不是很准确(尽管有时确实如此),所以希望您的自定义标记一开始会跳动一下。您可以等到位置管理器收集到一些坐标后再将其应用于您的自定义标记,方法是等到,locations.count > someNumber
但我认为这种方法不是很有吸引力。