【问题标题】:How to change google maps marker color when selected in swift?在swift中选择时如何更改谷歌地图标记颜色?
【发布时间】:2017-04-17 18:24:57
【问题描述】:

我有一个带有GMSMapView 的视图控制器,并在地图上加载了许多标记。我可以使用mapView.selectedMarker = ... 更改选择的标记,但是如何更改所选标记的颜色?

【问题讨论】:

    标签: ios swift google-maps google-maps-markers marker


    【解决方案1】:

    您可以使用GMSMarker.markerImage(with: <UIColor?>) 重置标记的图标。

    文档:Google Maps iOS SDK GMSMarker Class Reference

    import GoogleMaps
    
    // view controller
    class MapViewController: UIViewController {
    
        // outlets
        @IBOutlet weak var mapView: GMSMapView!
    
        // view did load method
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // set map view delegate
            mapView.delegate = self
        }
    }
    
    // extension for GMSMapViewDelegate
    extension MapViewController: GMSMapViewDelegate {
    
        // tap map marker
        func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
            print("didTap marker \(marker.title)")
    
            // remove color from currently selected marker
            if let selectedMarker = mapView.selectedMarker {
                selectedMarker.icon = GMSMarker.markerImage(with: nil)
            }
    
            // select new marker and make green
            mapView.selectedMarker = marker
            marker.icon = GMSMarker.markerImage(with: UIColor.green)
    
            // tap event handled by delegate
            return true
        }
    }
    

    【讨论】:

    • 如果你点击一个标记,然后点击地面,然后点击另一个标记,将会选择两个标记
    • 正如@duan 建议的那样,您的解决方案存在问题。
    • 是的,似乎当您手动设置选定的标记时,@duan 描述的行为就会发生。 google maps api 会自动为您设置选定的标记,因此不需要mapView.selectedMarker = marker 这一行。
    【解决方案2】:

    简单的方式 Swift 5

    marker.icon = GMSMarker.markerImage(with: UIColor.green)
    

    【讨论】:

      【解决方案3】:

      如果你使用 RxSwift,这里有一个优雅的解决方案,RxGoogleMaps

      Observable.combineLatest(mapView.rx.selectedMarker,
                               mapView.rx.selectedMarker.skip(1))
          .subscribe(onNext: { (old, new) in
              old?.icon = GMSMarker.markerImage(with: nil)
              new?.icon = GMSMarker.markerImage(with: UIColor.red)
          })
          .disposed(by: disposeBag)
      

      【讨论】:

      【解决方案4】:

      接受的答案对我不起作用,因为如果用户点击地图上的非标记,selectedMarker 将设置为 nil。如果用户随后点击了另一个标记,触发了 didTap 回调,则 selectedMarker 将为 nil 并因此保留其选定的状态/颜色。

      对我来说,解决方法是从 didTap 中删除 selectedMarker 逻辑并将其移动到 didCloseWindowOf。

      代码如下:

      func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
          marker.icon = UIImage(named: "map_marker_selected")
          return false // return false to display info window
      }
      
      func mapView(_ mapView: GMSMapView, didCloseInfoWindowOf marker: GMSMarker) {
          marker.icon = UIImage(named: "map_marker_unselected")
      }
      

      这是因为当用户点击非标记时,信息窗口会关闭,从而触发 didCloseInfoWindowOf。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-16
        • 1970-01-01
        • 2011-01-28
        • 2012-06-19
        • 2015-12-15
        • 1970-01-01
        • 2018-04-27
        • 1970-01-01
        相关资源
        最近更新 更多