【问题标题】:Geo Spacial Bounding Box Rectangle Calculation Error: Latitude Incorrect地理空间边界框矩形计算错误:纬度不正确
【发布时间】:2011-01-25 10:48:16
【问题描述】:

任何三角或 GPS 专家可以在这里帮助我吗?我正在尝试使用我检索到的以下方法创建一个地理空间边界框(矩形)计算,以返回最大纬度和经度。我为每个轴承调用一次该方法:北、南、东和西。使用这四个值,我打算在我的核心数据存储中查询框内的所有对象。

  -(CLLocation*) offsetLocation:(CLLocation*)startLocation:(double)offsetMeters:(double)bearing {


    double EARTH_MEAN_RADIUS_METERS = 6372796.99;
    double newLatitude = asin( sin(startLocation.coordinate.latitude) * cos(offsetMeters/EARTH_MEAN_RADIUS_METERS) + cos(startLocation.coordinate.latitude) * sin(offsetMeters/EARTH_MEAN_RADIUS_METERS) * cos(bearing) );
    double newLongitude = startLocation.coordinate.longitude + atan2( sin(bearing) * sin(offsetMeters/EARTH_MEAN_RADIUS_METERS) * cos(startLocation.coordinate.latitude), cos(offsetMeters/EARTH_MEAN_RADIUS_METERS) - sin(startLocation.coordinate.latitude) * sin(newLatitude));


    CLLocation *tempLocation = [[CLLocation alloc] initWithLatitude:newLatitude longitude:newLongitude];
    [tempLocation autorelease];
    return tempLocation;
}

问题是 newLatitude 偏移量的计算绝对不正确。鉴于以下情况:

startLocation:纬度37.331688999999997,经度-122.030731 偏移量:1000 方位:0(北)

newLatitude 返回 -0.36726592610659514(不正确)。

有什么建议吗?到目前为止,我已经围绕这个特定的公式进行了编码,这个公式让我很难过。我也尝试过从 PHP 翻译不同的公式,但无济于事。如果可以调整,我认为以上正是我所需要的。

谢谢, b.dot

【问题讨论】:

    标签: iphone objective-c core-data geolocation gps


    【解决方案1】:

    你们是如何设置 NSPredicates 的?

    我的似乎遇到了性能问题。

    NSPredicate *northWestLat = [NSPredicate predicateWithFormat:@"ANY locations.lat > %lf", northWestCorner.latitude];
    NSPredicate *southhWestLat = [NSPredicate predicateWithFormat:@"ANY locations.lat < %lf", southEastCorner.latitude];
    NSPredicate *latitudePredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:northWestLat, southhWestLat, nil]];
    
    NSPredicate *northWestLng = [NSPredicate predicateWithFormat:@"ANY locations.lng < %lf", northWestCorner.longitude];
    NSPredicate *southEastLng = [NSPredicate predicateWithFormat:@"ANY locations.lng > %lf", southEastCorner.longitude];
    NSPredicate *longitudePredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:northWestLng, southEastLng, nil]];
    
    NSPredicate *coordPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:latitudePredicate, longitudePredicate, nil]];
    

    【讨论】:

    • 你为什么使用any?为什么不只是[NSPredicate predicateWithFormat:@"latitude &gt; %f AND latitude &lt; %f AND longitude &gt; %f AND longitude &lt; %f", latMin, latMax, lngMin, lngMax];
    【解决方案2】:

    CLLocationCoordinate2D 以度为单位存储坐标,但您使用的三角函数需要弧度单位。如果转换为弧度(乘以 M_PI/180 或 0.017453293f),它可能会起作用。

    【讨论】:

      【解决方案3】:

      我没有看过你的代码,但你也可以使用 MapKit 函数 MKCoordinateRegionMakeWithDistance() 让框架为你计算一个边界框。

      CLLocationCoordinate2D center = { 37.3, -122.0 };
      MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, 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);
      

      【讨论】:

      • 哇。我没有使用 MapKit,所以我没想到那里。谢谢。这两种方法我都会尝试。
      • MKCoordinateRegionMakeWithDistance 很容易制作一个漂亮的盒子,但是我不确定如何检索新创建区域的边界(最大纬度或经度)。所有属性似乎都与 MapView 本身有关。
      • 这只是一个小数学,不是吗?我在答案中添加了一些代码。请注意,如果边界框越过日期变更线或两极,则此代码不起作用。但是,无论如何,您都需要将其拆分为多个框来查询数据存储。
      • 奥莱,谢谢!。它完美地工作。这是我第一次使用 MapKit,我从来没有想过你可以用简单的数学来做到这一点。我花了两天时间试图理解 Haversine 和 Vincenty 公式,直到我的大脑一片混乱。现在,这是“一点数学”。 :)
      猜你喜欢
      • 1970-01-01
      • 2020-05-04
      • 1970-01-01
      • 2010-11-21
      • 2011-03-06
      • 1970-01-01
      • 2013-01-16
      • 2012-07-10
      • 1970-01-01
      相关资源
      最近更新 更多