ios - Google Maps 为点击和未选择状态绘制不同的标记图像

标签 ios swift google-maps swift4 google-maps-sdk-ios

有没有办法添加一个 ID、一个案例或类似的东西,以便为选中和未选中状态设置自定义标记图像?

我有多个具有不同名称/标题的标记,并且我需要特定类型的标记具有另一个用于选中和未选中状态的图像。

例如:

marker1 - marker_1_image

marker2 - marker_2_image

Atm 我只能用正确的图像设置 2 个图像,但如果我点击一个标记,它将继承在“.contains”函数上设置的特定类型。
我做了一个“.contains”函数,以便在 POI/位置名称下的 JSON 响应中找到特定字符串。例如:currentAtmModel?.placeName?.contains("restaurant"),因此名称为“restaurant”的标记将与特定的图标图像一起显示。

我正在使用适用于 iOS 的最新版本的 Google Maps SDK。

这是我的代码:

var markersModel: [MarkersLocation] = [] {
        didSet {
            updateAllMarkers()
        }
    }

private func updateAllMarkers() {
        for selectedPOIMarker in markersModel {
            let currentPOIModel = selectedPOIMarker.location
            let latitude = currentPOIModel?.coordinates?.latitude
            let longitude = currentPOIModel?.coordinates?.longitude

            let marker = GMSMarker()
            marker.appearAnimation = GMSMarkerAnimation.pop
            marker.position = CLLocationCoordinate2D(latitude: latitude ?? 0, longitude: longitude ?? 0)

            if (currentPOIModel?.placeName?.contains("db")) ?? false {
                marker.icon = restaurantMarkerIcon(selected: false)
            } else {
                marker.icon = normalMarkerIcon(selected: false)
            }
            marker.map = googleMapsView
        }
    }

extension POILocatorViewController: GMSMapViewDelegate {
    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        googleMapsView.selectedMarker = marker
        googleMapsView.animate(toLocation: marker.position)
        googleMapsView.selectedMarker?.icon = normalMarkerIcon(selected: true)
        googleMapsView.selectedMarker?.icon = restaurantMarkerIcon(selected: true)
        guard let markersModel = markersModel.filter({ $0.location?.coordinates?.latitude == marker.position.latitude && $0.location?.coordinates?.longitude == marker.position.longitude }).first else { return false }
        coordinatorDelegate?.poiLocatorViewController(self, didTap: markersModel, currentLocation: currentLocation)

        return true
    }
}

extension POILocatorViewController {
    func reloadMarker() {
        googleMapsView.selectedMarker?.icon = normalMarkerIcon(selected: false)
        googleMapsView.selectedMarker?.icon = restaurantMarkerIcon(selected: false)
        googleMapsView.selectedMarker = nil
    }
}

最佳答案

何时将标记添加到 GMSMapView ,您可以为 GMSMarker 分配唯一标识符使用它是zIndex属性来识别标记。

 let marker = GMSMarker()
 marker.appearAnimation = GMSMarkerAnimation.pop
 marker.position = CLLocationCoordinate2D(latitude: latitude ?? 0, longitude: longitude ?? 0)

 if (currentPOIModel?.placeName?.contains("db")) ?? false {
     marker.icon = restaurantMarkerIcon(selected: false)
 } else {
     marker.icon = normalMarkerIcon(selected: false)
 }
 marker.zIndex = Int32(id) //your marker unique id
 marker.map = googleMapsView

然后您可以在 didTap 中的标记中获取标记唯一标识符基于标记设置不同图像或执行其他操作的方法
extension POILocatorViewController: GMSMapViewDelegate {
    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {

        print("Marker Identifier = \(marker.zIndex)") //Get your marker your unique identifier here.
        return true
    }
}

希望这对您有所帮助。

关于ios - Google Maps 为点击和未选择状态绘制不同的标记图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59870181/

相关文章:

ios - 在 iOS 中调用多个服务的更好方法是什么?

ios - 在另一个类中调用函数的最佳方式是什么? ( swift )

swift - 使用 Google Maps API 在 Swift 3 中查找 2 个位置之间的驾驶/步行距离

ios - NSInvalidArgumentException',原因 : 'Application tried to present a nil modal view controller on target

iphone - NSAutoLayoutConstraints 中 Constraints 属性的含义

ios - SKPaymentTransaction 下载数组始终为零?

swift - SpriteKit - 获取 SKSpriteNode 的权重

performance - 谷歌地图是如何工作的?

android - adMob.jar 库和 google play service.jar 不能一起工作

ios - 在 phonegap 开发者应用程序中使用 iframe