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
usesMKMapPoint
units which are not the same thing asCLLocationDegrees
.The
MKMapRectMake
function needs the top-leftMKMapPoint
and then the width and height (again inMKMapPoint
units).Basically, you need to use the the
MKMapPointForCoordinate
function to help you do this conversion from degrees toMKMapPoint
units.First, you could construct an
MKCoordinateRegion
and then convert it to anMKMapRect
.For example:
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).