1
votes

I am working with an API that allows me to specify a "bounding box" of coordinate which allows me to only return results within that box:

  • Returns a list of geocaches inside the specified bounding box sorted by the distance from the center of the box.
  • The parameters south latitude, west longitude, north latitude, east longitude define the edges of a bounding box.
  • Coordinates should be in decimal degrees Use positive numbers for north latitude and east longitude and negative numbers of south latitude and west longitude.
  • The box cannot cross the 180° longitude line or the 90° or -90° points.

The math for this is a little beyond me, but I found somewhat helpful calculations, but I am not sure it is what I need for the API:

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapView.centerCoordinate, 2000.0, 2000.0);
CLLocationCoordinate2D northWestCorner, southEastCorner;
northWestCorner.latitude  = center.latitude  - (region.span.latitudeDelta  / 2.0);
northWestCorner.longitude = center.longitude + (region.span.longitudeDelta / 2.0);
southEastCorner.latitude  = center.latitude  + (region.span.latitudeDelta  / 2.0);
southEastCorner.longitude = center.longitude - (region.span.longitudeDelta / 2.0);

Does anyone know how I could do this? Are the calculations here not helpful in order to get the west longitude, north latitude, east longitude that define the edges of the bounding box?

EDIT:

The error I am getting:

Invalid value for parameter: bbox=south,west,north,east

Using the center value:

center=37.552821,-122.377413

Converted Box (after calculations from above):

bbox=37.561831,-122.388730,37.543811,-122.366096

Final Working code:

// Current distance
MKMapRect mRect = mapView.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));
CLLocationDistance distance = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);

// Region
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(request.center, distance, distance);
CLLocationCoordinate2D northWestCorner, southEastCorner;
northWestCorner.latitude  = request.center.latitude  + (region.span.latitudeDelta  / 2.0);
northWestCorner.longitude = request.center.longitude - (region.span.longitudeDelta / 2.0);
southEastCorner.latitude  = request.center.latitude  - (region.span.latitudeDelta  / 2.0);
southEastCorner.longitude = request.center.longitude + (region.span.longitudeDelta / 2.0);
base = [base stringByAppendingFormat:@"bbox=%f,%f,%f,%f&", southEastCorner.latitude,northWestCorner.longitude,northWestCorner.latitude,southEastCorner.longitude];
1

1 Answers

2
votes

You seem to have gotten your hemispheres reversed. North and east are positive. So if you start from the center latitude and you want to find the northern boundary you ADD half the delta, not subtract.