【问题标题】:How set minClusterSize in google map?如何在谷歌地图中设置 minClusterSize?
【发布时间】:2017-04-19 21:41:16
【问题描述】:

在 Android SDK 中有 minClusterSize 属性,当我们将其设置为 1 时,它会将每个元素显示为集群而不是标记。 但在 iOS SDK 中不存在 minClusterSize 和 DefaultRenderer 默认显示低于 3 个项目作为项目不集群。 有什么解决办法吗? 谢谢。

【问题讨论】:

  • 但是如果你想在地图上显示每一个标记,为什么要使用集群呢?

标签: ios swift google-maps markerclusterer google-maps-ios-utils


【解决方案1】:
import UIKit

class CustomClusterRenderer: GMUDefaultClusterRenderer {

    let GMUAnimationDuration: Double = 0.5
    var mapView: GMSMapView?

    override init(mapView: GMSMapView, clusterIconGenerator iconGenerator: GMUClusterIconGenerator) {
        super.init(mapView: mapView, clusterIconGenerator: iconGenerator)
        self.mapView = mapView
    }

    func markerWith(position: CLLocationCoordinate2D, from: CLLocationCoordinate2D, userData: AnyObject, clusterIcon: UIImage, animated: Bool) -> GMSMarker {

        let initialPosition = animated ? from : position
        let marker = GMSMarker(position: initialPosition)
        marker.userData = userData

        marker.icon = clusterIcon
        marker.groundAnchor = CGPoint(x: 0.5, y: 0.5)

        marker.map = mapView
        if animated {
            CATransaction.begin()
            CAAnimation.init().duration = GMUAnimationDuration
            marker.layer.latitude = position.latitude
            marker.layer.longitude = position.longitude
            CATransaction.commit()
        }
        return marker
    }

    func getCustomUIImageItem(userData: AnyObject) -> UIImage {
        if let item = userData as? Marker {
            return item.merkerIcon
        }
        return UIImage()
    }

    override func shouldRender(as cluster: GMUCluster, atZoom zoom: Float) -> Bool {
        print("Zoom Level is \(zoom) , and result is \(zoom<=14)")
        return zoom <= 14;
    }


}


class Marker: NSObject, GMUClusterItem {

    var position: CLLocationCoordinate2D
    var estate: Estate

    init(estate: Estate) {

        self.estate = estate
        self.position = CLLocationCoordinate2D(latitude: estate.latitude,longitude: estate.longitude)

    }

}

【讨论】:

    【解决方案2】:

    在 GMUDefaultClusterRenderer.m 类上添加一行

     static const NSUInteger kGMUMinClusterSize = 10;
    

    【讨论】:

      猜你喜欢
      • 2010-12-05
      • 2016-01-16
      • 1970-01-01
      • 2014-02-18
      • 2013-08-04
      • 2018-08-18
      • 2011-02-05
      • 2012-07-12
      • 2020-01-09
      相关资源
      最近更新 更多