Annotation Clustering in iPhone MKMapView

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 22:26:31

问题


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.


回答1:


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. 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).

  2. 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);



回答2:


MKMapView on iOS CCHMapClusterController

this might helps you :)



来源:https://stackoverflow.com/questions/28957431/annotation-clustering-in-iphone-mkmapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!