【问题标题】:Custom user location dot in Google maps for iOS (GMSMapview)iOS 版 Google 地图中的自定义用户位置点 (GMSMapview)
【发布时间】:2015-06-23 09:04:11
【问题描述】:
  1. 是否有官方方法可以在 iOS 版 Google 地图 (GMSMapView) 中设置自定义用户位置点?
  2. 有已知的“破解”方法吗?喜欢遍历所有子视图和图层并找出蓝点?
  3. 即使你不能自定义它的外观,你能控制它的z顺序索引吗?当您有许多标记时,小蓝点会被隐藏,有时您希望它始终可见。

谢谢

【问题讨论】:

  • 如果您使用的是Google Maps iOS SDK,似乎无法替换默认的用户位置图标,解决方法是在用户当前位置上放置一个标记,您可以查看this answer了解更多信息详情。

标签: ios objective-c google-maps google-maps-sdk-ios gmsmapview


【解决方案1】:

您可以尝试在以下位置找到图片:

GoogleMaps.framework > 资源 > GoogleMaps.bundle 要么 GoogleMaps.framework > 资源 > GoogleMaps.bundle > GMSCoreResources.bundle

我对这些文件进行了快速搜索,发现与该蓝点相关的唯一文件是 GMSSprites-0-1x。

请阅读谷歌地图条款和条件,因为这可能不合法。

【讨论】:

  • 谁能确认 Google 是否在使用自定义颜色更新蓝点和精度圈时遇到问题?因为它可以以编程方式完成并且具有相同的效果......
【解决方案2】:

您可以将地图myLocationEnabled 设置为NO。这将隐藏默认位置点。然后使用CLLocationManager 的实例为您提供职位。在CLLocationManagerdidUpdateLocations 方法中,您可以设置自定义GMSMarker。使用[UIImage imageNamed:] 将其图标属性设置为您希望您的点看起来像的任何东西。这将使您达到预期的效果。

【讨论】:

  • 有这样的例子吗?
  • 不,那么当用户开始在这里和那里移动时就会出现问题,那么就会出现多个标记。
  • @G.Abhisek : 你必须改变标记的位置.. 你不必在每个动作上添加标记
【解决方案3】:

斯威夫特 4

禁用默认的谷歌地图当前位置标记(默认禁用):

mapView.isMyLocationEnabled = false

创建一个标记作为视图控制器的实例属性(因为委托需要访问它):

let currentLocationMarker = GMSMarker()

GMSMarker 初始化器允许将 UIImageUIView 作为自定义图形,不幸的是,UIImageView 不是。如果您想对图形进行更多控制,请使用UIView。在您的 loadViewviewDidLoad(无论您在哪里配置地图)中,配置标记并将其添加到地图中:

// 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,但我不觉得这种方法很有吸引力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多