【问题标题】:Customize Google Maps blue dot for current location为当前位置自定义谷歌地图蓝点
【发布时间】:2013-08-22 06:29:06
【问题描述】:

我使用的是 2013 版的 iOS 版 Google Maps SDK。我想为当前位置自定义默认蓝点,并使用另一个图标或周围的脉动圆圈。

我知道我们可以在 MKMapView 中使用 mapView:viewForAnnotation: 做到这一点,但我不知道如何使用 Google 地图做到这一点。

【问题讨论】:

    标签: ios google-maps google-maps-markers google-maps-sdk-ios currentlocation


    【解决方案1】:

    当前版本的 SDK (1.4.3) 无法执行此操作,实际上此请求存在一个未解决的问题:Look here。

    作为一种解决方法,您可以使用以下方法隐藏默认按钮:

     _map.myLocationEnabled = NO;
    

    然后创建一个自定义GMSMarker

     GMSMarker *pointMarker = [GMSMarker markerWithPosition:currentPosition];
     pointMarker.icon = [UIImage imageNamed:@"YourImage"];
     pointMarker.map = _map;
    

    并使用CLLocationManager 更改它的位置,因此它始终显示当前位置。这有点棘手,但我认为这是实现这一目标的唯一方法。如果您需要更完整的示例,请告诉我。

    【讨论】:

    • 是的,我试过了,但它在用户当前位置周围显示了多个标记。请给出更详细的代码。
    • 对不起,我已经很久没有使用Gmaps了,我不确定这个答案是否仍然有效。无论如何,如果您有问题,请提出一个新问题,我无法帮助您解决修改旧答案的问题。
    • 感谢您的回答。这对我帮助很大。
    【解决方案2】:

    对于 Swift 4.0

    当您设置 GMSMapView 时:

    mapView.isMyLocationEnabled = false
    

    当用户位置更新时:

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
        let userLocationMarker = GMSMarker(position: location.coordinate)
        userLocationMarker.icon = UIImage(named: "yourImageName")
        userLocationMarker.map = mapView   
    }
    

    【讨论】:

      【解决方案3】:

      斯威夫特 4

      class MasterMapViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
      
          let currentLocationMarker = GMSMarker()
      
          override func viewDidLoad() {
              super.viewDidLoad()
              addCurrentLocationMarker()
          }
      
          func addCurrentLocationMarker() {
      
              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)
              currentLocationMarker.iconView = currentLocationMarkerView
              currentLocationMarker.isTappable = false
              currentLocationMarker.map = mapView
      
          }
      
          // location manager delegate
          func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      
              let lastLocation = locations.last!
              currentLocationMarker.position =  lastLocation.coordinate    
          }
      
      }
      

      仅以此作为起点!这不是一个有吸引力的替代方案,因为不断更新标记在地图上的位置会影响性能。如果您要走这条路线,请找到一种方法,不要不断地从位置管理器代理那里更新标记的位置。

      【讨论】:

      • currentLocationMarker?.icon = UIImage(named: "driver_circle")!.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: UIImage(named: "driver_circle")!.size.height /2,右:0)) 我花了几天时间才理解的快速修复!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      相关资源
      最近更新 更多