【问题标题】:Finding Bounding Box from CLLocationCoordinate2D从 CLLocationCoordinate2D 中找到边界框
【发布时间】:2013-05-06 01:44:44
【问题描述】:

我正在使用一个 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];

【问题讨论】:

    标签: ios objective-c mapkit cllocation mkcoordinateregion


    【解决方案1】:

    你的大脑半球似乎颠倒了。北部和东部是积极的。因此,如果您从中心纬度开始,并且想要找到北部边界,则添加一半的增量,而不是减去。

    【讨论】:

    • 所以,你是说我需要将我所有的加号反转为减号,反之亦然?
    • 是的,这将为您提供正确的 northWestCorner 和 southEastCorner。你试过了吗?
    • 是的,我尝试修复你所说的,然后使用字符串base = [base stringByAppendingFormat:@"bbox=%f,%f,%f,%f&", northWestCorner.latitude,northWestCorner.longitude, southEastCorner.latitude,southEastCorner.longitude];,它仍然给我一个错误。您可以发布您认为需要的代码吗?我不确定我的理解是否正确。
    • 您如何发布您遇到的错误。连同打印出您的region 中的值。编辑原始问题,评论框中没有足够的空间来显示您需要显示的内容。
    • 你的边界框是颠倒的。看看它是如何期望“南、西、北、东”的,但你给它的是北、西、南、东。
    猜你喜欢
    • 2021-04-15
    • 2019-10-13
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 2013-10-25
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多