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
MKMapRectusesMKMapPointunits which are not the same thing asCLLocationDegrees.The
MKMapRectMakefunction needs the top-leftMKMapPointand then the width and height (again inMKMapPointunits).Basically, you need to use the the
MKMapPointForCoordinatefunction to help you do this conversion from degrees toMKMapPointunits.First, you could construct an
MKCoordinateRegionand then convert it to anMKMapRect.For example:
The
mapRectForCoordinateRegionmethod 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,
annotationsInMapRectwill include the annotation that you are searching around (since you are using it as the center).