MKMapView中的注释聚类

MKMapView中的注释聚类

本文介绍了iPhone MKMapView中的注释聚类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MKMap中使用群集,但不希望使用第三方框架.因此,我从 https://developer.apple下载代码.com/library/ios/samplecode/PhotoMap/Introduction/Intro.html 但是我发现,当我随机旋转和缩放地图时,它卡住了.如果您还有其他演示,请帮助我.

I want to use clustering in MKMap but not by using 3rd party framework.So for that I download the code from https://developer.apple.com/library/ios/samplecode/PhotoMap/Introduction/Intro.htmlbut I found that when I am rotating and zooming the map randomly it got stuck. if you have any other demo then please help me.

推荐答案

我在示例代码中也遇到了问题.现有代码不适用于可旋转的地图.

I just encountered the problem in that example code too. The existing code doesn't work with maps that can be rotated.

有两种情况会导致很长的循环或无限的循环.

There are two situations that lead to very long or infinite loops.

  1. 如果将地图旋转180度,最终将出现mapView左侧的经度大于mapView右侧的经度的情况.如果leftCoordinate大于rightCoordinate,则gridSize变为负数.在while循环中,我们将地图矩形的原点增加gridSize,直到它大于endX/endY.但是,如果gridSize为负,则原点实际上会变小,并且不会达到endX条件(没有算术下溢).

  1. If you rotate the map 180 degrees, you will end up with a situation where the longitude on the left side of the mapView is larger than the longitude on the right side of the mapView. And if leftCoordinate is larger than rightCoordinate, gridSize becomes negative. In the while loops, we increase the origin of the map rect bygridSize until it's larger than endX/endY. But if gridSize is negative, the origin will actually become smaller, and the endX condition won't be reached (without an arithmetic underflow).

如果将地图旋转90或270度,最终将得到两个非常相似的经度,因此gridSize将非常小甚至为0,并且循环会花费很长时间(或永远为0)完成.

If you rotate the map 90 or 270 degrees, you will end up with two longitudes that are very similar, so gridSize will be very small or even 0, and the loops take a long time (or in case of 0 forever) to complete.

可以使用gridSize上的abs()解决第一个问题.第二个问题需要更改rightCoordinate的计算,因此它使用点bucketSize, bucketSize而不是bucketSize, 0.完成此操作后,我们将当前的gridSize变量更改为gridSizeX,并引入一个使用MapPoints的.y部分的gridSizeY.

The first problem can be fixed by using abs() on gridSize. The second problem requires to change the calculation of rightCoordinate so it uses point bucketSize, bucketSize instead of bucketSize, 0. Once that is done we change our current gridSize variable to gridSizeX, and introduce a gridSizeY that uses the .y parts of the MapPoints.

这是原始代码:

// 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;

将由以下内容替换:

// 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);

这篇关于iPhone MKMapView中的注释聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:29