ios - Finding Bounding Box from CLLocationCoordinate2D -


i working api allows me specify "bounding box" of coordinate allows me return results within box:

  • returns list of geocaches inside specified bounding box sorted distance center of box.
  • the parameters south latitude, west longitude, north latitude, east longitude define edges of bounding box.
  • coordinates should in decimal degrees use positive numbers north latitude , east longitude , negative numbers of south latitude , west longitude.
  • the box cannot cross 180° longitude line or 90° or -90° points.

the math little beyond me, found helpful calculations, not sure need 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 know how this? calculations here not helpful in order west longitude, north latitude, east longitude define edges of bounding box?

edit:

the error getting:

invalid value parameter: bbox=south,west,north,east

using center value:

center=37.552821,-122.377413

converted box (after calculations 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]; 

you seem have gotten hemispheres reversed. north , east positive. if start center latitude , want find northern boundary add half delta, not subtract.


Comments