Mapbox如何在点击它时解散集群

时间:2014-06-17 10:43:14

标签: ios xcode map mapbox markerclusterer

我在我的项目中使用Mapbox sdk。 https://www.mapbox.com/mapbox-ios-sdk/

我已经完成了基本的群集,但我的问题是如何在点击它时进一步解散群集。

例如。我有一个包含8个标记的簇。单击它时,它应该进一步放大,而不仅仅是一个级别,而是放在屏幕上所有8个标记都可以进行最大放大的位置。(这些标记中的一些标记可以聚类)

我试过了 [mapView zoomWithLatitudeLongitudeBoundsSouthWest:CLLocationCoordinate2DMake(南,西)                                                 northEast:CLLocationCoordinate2DMake(北,东)动画:是]; 但没有成功。

4 个答案:

答案 0 :(得分:1)

这就是结束的方式:

- (void)tapOnAnnotation:(RMAnnotation *)annotation onMap:(RMMapView *)map {

    if (annotation.isClusterAnnotation) {

        CLLocationCoordinate2D southwestCoordinate = annotation.coordinate;
        CLLocationCoordinate2D northeastCoordinate = annotation.coordinate;

        for (RMAnnotation *plot in annotation.clusteredAnnotations) {

            CGFloat latititude = plot.coordinate.latitude;
            CGFloat longitude = plot.coordinate.longitude;

            if (southwestCoordinate.latitude > fabsf(latititude)) southwestCoordinate.latitude = latititude;
            if (southwestCoordinate.longitude > fabsf(longitude)) southwestCoordinate.longitude = longitude;

            if (northeastCoordinate.latitude < fabsf(latititude)) northeastCoordinate.latitude = latititude;
            if (northeastCoordinate.longitude < fabsf(longitude)) northeastCoordinate.longitude = longitude;

        }

        [self.mapView zoomWithLatitudeLongitudeBoundsSouthWest:southwestCoordinate northEast:northeastCoordinate animated:YES];

   }
}

基本上,这里发生的是我将最外层区域的边界坐标southwestCoordinatenortheastCoordinate存储到抽头注释(在本例中为群集)。然后,对于集群中的每个注释,我们检查它与该“中心”坐标的绝对距离是否是该组中最大的。

似乎对我来说效果很好。

答案 1 :(得分:1)

以下代码段可以更好地放大群集点数,扩展到某些或所有点都未聚集的水平:

    // 1. Start with a maximum (world-wide) bounding box
    CLLocationCoordinate2D topRight = CLLocationCoordinate2DMake(180, -90);
    CLLocationCoordinate2D bottomLeft = CLLocationCoordinate2DMake(-180, 90);

    // 2. Derive the minimum bounding box coordinates that contains all cluster points, by "squeezing in" on their coordinates
    for (RMAnnotation *a in annotation.clusteredAnnotations) {

        if (a.coordinate.latitude < topRight.latitude) {
            topRight.latitude = a.coordinate.latitude;
        }
        if (a.coordinate.longitude > topRight.longitude) {
            topRight.longitude = a.coordinate.longitude;
        }

        if (a.coordinate.latitude > bottomLeft.latitude) {
            bottomLeft.latitude = a.coordinate.latitude;
        }
        if (a.coordinate.longitude < bottomLeft.longitude) {
            bottomLeft.longitude = a.coordinate.longitude;
        }
    }

    CLLocation *sw = [[CLLocation alloc] initWithLatitude:topRight.latitude longitude:topRight.longitude];
    CLLocation *ne = [[CLLocation alloc] initWithLatitude:bottomLeft.latitude longitude:bottomLeft.longitude];

    // 3. Calculate the distance in meters across the calculated bounding box
    CLLocationDistance distanceInMeters = [ne distanceFromLocation:sw];
    // 4. Adjust the map view's meters per pixel setting so that the bounding box fits nicely within the map views bounds (which is equivalent to zooming)
    [self.mapView setMetersPerPixel:(distanceInMeters / (self.mapView.frame.size.width * 0.7)) animated:YES];
    // 5. Center on the midpoint of the bounding box
    [self.mapView setCenterCoordinate:[self midPointBetween:topRight and:bottomLeft] animated:YES];

答案 2 :(得分:1)

Google地图

已使用:https://github.com/googlemaps/google-maps-ios-utils用于群集

&#34; Uncluster cluster&#34; :d

Swift 3

if let cluster = marker.userData as? GMUCluster {
let boxConstant = 0.01
    var southwestCoordinate = cluster.position
    var northeastCoordinate = cluster.position

    for clusterItem in cluster.items {

      let latitude = clusterItem.position.latitude
      let longitude = clusterItem.position.longitude

      if southwestCoordinate.latitude > latitude {
        southwestCoordinate.latitude = latitude
      }
      if southwestCoordinate.longitude > longitude {
        southwestCoordinate.longitude = longitude
      }
      if northeastCoordinate.latitude < latitude {
        northeastCoordinate.latitude = latitude
      }
      if northeastCoordinate.longitude < longitude {
        northeastCoordinate.longitude = longitude
      }

    }

    // Create Box
    southwestCoordinate.longitude += boxConstant
    southwestCoordinate.latitude -= boxConstant
    northeastCoordinate.longitude -= boxConstant
    northeastCoordinate.latitude += boxConstant
    let bounds = GMSCoordinateBounds(coordinate: southwestCoordinate, coordinate: northeastCoordinate)
    self.mapView.moveCamera(GMSCameraUpdate.fit(bounds))
}

答案 3 :(得分:0)

我会将此添加为元数据答案的评论,但我没有足够的代表。

我认为,对于那个fasbf,它会对负坐标进行错误的计算,如果没有它,它会更好。我是对的吗?

- (void)tapOnAnnotation:(RMAnnotation *)annotation onMap:(RMMapView *)map {
    if (annotation.isClusterAnnotation) {

        CLLocationCoordinate2D southwestCoordinate = annotation.coordinate;
        CLLocationCoordinate2D northeastCoordinate = annotation.coordinate;

        for (RMAnnotation *plot in annotation.clusteredAnnotations) {

            CGFloat latitude = plot.coordinate.latitude;
            CGFloat longitude = plot.coordinate.longitude;

            if (southwestCoordinate.latitude > latitude) southwestCoordinate.latitude = latitude;
            if (southwestCoordinate.longitude > longitude) southwestCoordinate.longitude = longitude;

            if (northeastCoordinate.latitude < latitude) northeastCoordinate.latitude = latitude;
            if (northeastCoordinate.longitude < longitude) northeastCoordinate.longitude = longitude;

        }

        [map zoomWithLatitudeLongitudeBoundsSouthWest:southwestCoordinate northEast:northeastCoordinate animated:YES];    
    }
}