Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift中选择时,如何更改谷歌地图标记颜色?_Ios_Swift_Google Maps_Google Maps Markers_Marker - Fatal编程技术网

Ios 在swift中选择时,如何更改谷歌地图标记颜色?

Ios 在swift中选择时,如何更改谷歌地图标记颜色?,ios,swift,google-maps,google-maps-markers,marker,Ios,Swift,Google Maps,Google Maps Markers,Marker,我有一个带有GMSMapView的视图控制器,并在地图上加载了许多标记。我可以使用mapView.selectedMarker=…更改选择的标记,但如何更改所选标记的颜色?您可以使用GMSMarker.markerImage(带:)重置标记的图标 文件: 如果您使用RxSwift,这里有一个优雅的解决方案 捷径5 marker.icon = GMSMarker.markerImage(with: UIColor.green) 接受的答案对我不起作用,因为如果用户点击地图上的非标记,select

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

您可以使用
GMSMarker.markerImage(带:)
重置标记的图标

文件:


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

捷径5

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

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

我的解决办法是从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。

如果点击一个标记,然后点击地面,然后点击另一个标记,当@duan建议您的解决方案存在问题时,将选择两个标记。是的,当您手动设置所选标记时,似乎会出现@duan描述的行为。google maps api自动为您设置所选标记,因此无需使用此行
mapView.selectedMarker=marker
。他们的文档中还有一个官方示例,它使用
Observable.zip
而不是
Observable.CombineTest
marker.icon = GMSMarker.markerImage(with: UIColor.green)
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")
}