Reputation: 420
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.html but 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.
Upvotes: 0
Views: 1221
Reputation: 90117
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.
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).
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.
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.
This is the original code:
// 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;
Which will be replaced by this:
// 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);
Upvotes: 3