I'm trying to get all annotations around another annotation on a specified area, but I can't figure how to do it. Now I'm trying with:
MKMapRect mapRect = MKMapRectMake(annotation.coordinate.longitude, annotation.coordinate.latitude, 10.0, 10.0);
NSSet *nearbyAnnotations = [map annotationsInMapRect:mapRect];
but nearbyAnnotations is empty. I tried by swapping longitude with latitude and also with bigger numbers for the 3rd and 4th parameters, but still no result. How I should do this?
An MKMapRect
uses MKMapPoint
units which are not the same thing as CLLocationDegrees
.
The MKMapRectMake
function needs the top-left MKMapPoint
and then the width and height (again in MKMapPoint
units).
Basically, you need to use the the MKMapPointForCoordinate
function to help you do this conversion from degrees to MKMapPoint
units.
First, you could construct an MKCoordinateRegion
and then convert it to an MKMapRect
.
For example:
//create a region 10km around the annotation...
MKCoordinateRegion mapRegion = MKCoordinateRegionMakeWithDistance
(annotation.coordinate, 10000, 10000);
//convert the MKCoordinateRegion to an MKMapRect...
MKMapRect mapRect = [self mapRectForCoordinateRegion:mapRegion];
The mapRectForCoordinateRegion
method is something you have to write.
For an example of one way to write it, see this answer:
How to make the union between two MKCoordinateRegion
By the way, note that in your case, annotationsInMapRect
will include the annotation that you are searching around (since you are using it as the center).