iPhone MKMapView中的注释聚类
我想在MKMap中使用聚类,而不是使用第三方框架。 因此,我从https://developer.apple.com/library/ios/samplecode/PhotoMap/Introduction/Intro.html下载了代码,但是我发现当我旋转并随机缩放地图时,卡住了。 如果你有任何其他的演示,那么请帮助我。
我刚刚在该示例代码中遇到了问题。 现有的代码不适用于可以旋转的地图。
有两种情况会导致非常长或无限循环。
-
如果将地图旋转180度,则最终会出现mapView左侧的经度大于mapView右侧的经度的情况。 如果
leftCoordinate
比rightCoordinate
,gridSize
变成负值。 在while循环中,我们通过gridSize
增加map rect的gridSize
直到它大于endX
/endY
。 但是,如果gridSize
为负值,则原点实际上会变小,并且不会达到endX条件(没有算术下溢)。 -
如果将地图旋转90度或270度,则最终会出现两个非常相似的经度,所以gridSize将会非常小,甚至是0,并且循环需要很长时间(或永远为0)才能完成。
第一个问题可以通过在gridSize
上使用abs()
来gridSize
。 第二个问题需要更改rightCoordinate
的计算,因此它使用的是rightCoordinate
, rightCoordinate
而不是rightCoordinate
bucketSize, 0
。 一旦完成,我们将当前gridSize
variables更改为gridSizeX
,并引入使用MapPoint的.y部分的gridSizeY
。
这是原始的代码:
// PhotoMapViewController.m, line 199+ // determine how wide each bucket will be, as a MKMapRect square CLLocationCoordinate2D leftCoordinate = [self.mapView convertPoint:CGPointZero toCoordinateFromView:self.view]; CLLocationCoordinate2D rightCoordinate = [self.mapView convertPoint:CGPointMake(bucketSize, 0) toCoordinateFromView:self.view]; double gridSize = MKMapPointForCoordinate(rightCoordinate).x - MKMapPointForCoordinate(leftCoordinate).x;
这将被replace为:
// determine how wide each bucket will be, as a MKMapRect square CLLocationCoordinate2D leftCoordinate = [self.mapView convertPoint:CGPointZero toCoordinateFromView:self.view]; CLLocationCoordinate2D rightCoordinate = [self.mapView convertPoint:CGPointMake(bucketSize, bucketSize) toCoordinateFromView:self.view]; double gridSizeX = fabs(MKMapPointForCoordinate(rightCoordinate).x - MKMapPointForCoordinate(leftCoordinate).x); double gridSizeY = fabs(MKMapPointForCoordinate(rightCoordinate).y - MKMapPointForCoordinate(leftCoordinate).y); double gridSize = MAX(gridSizeX, gridSizeY);
iOS上的MKMapView CCHMapClusterController
这可能会帮助你:)