簡體   English   中英

從CLLocationCoordinate2D查找邊界框

[英]Finding Bounding Box from CLLocationCoordinate2D

我正在使用一個API,該API允許我指定坐標的“邊界框”,該坐標僅允許我在該框內返回結果:

  • 返回指定邊界框內的地理緩存列表,該列表按距框中心的距離排序。
  • 參數南緯,西經,北緯,東經定義邊界框的邊緣。
  • 坐標應為十進制度數。北緯和東經使用正數,南緯和西經使用負數。
  • 盒子不能越過180度經度線或90度或-90度點。

算術運算超出了我,但我發現了一些有用的計算,但是我不確定這是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);

有人知道我該怎么做嗎? 為了獲得定義邊界框邊緣的west longitude, north latitude, east longitude此處的計算是否有用?

編輯:

我得到的錯誤:

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

使用中心值:

center=37.552821,-122.377413

轉換后的盒子(經過上面的計算):

bbox=37.561831,-122.388730,37.543811,-122.366096

最終工作代碼:

// 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];

您似乎已經扭轉了半球。 北方和東方是積極的。 因此,如果您從中心緯度開始,並且想要找到北邊界,則可以將三角洲增加一半,而不是減去。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM