地理空间边界框矩形计算错误:纬度不正确

时间:2010-03-09 13:51:02

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

任何触发或GPS专家可以帮助我吗?我正在尝试创建一个地理空间边界框(矩形)计算,使用我检索到的以下方法返回最大纬度和经度。我为每个轴承调用一次方法:北,南,东和西。有了这四个值,我打算在我的Core Data商店中查询框中的所有对象。

  -(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   offsetMeters:1000   方位:0(北)

newLatitude返回-0.36726592610659514(不正确)。

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

谢谢, b.dot

3 个答案:

答案 0 :(得分:13)

我没有看过你的代码,但你也可以使用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);

答案 1 :(得分:1)

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

答案 2 :(得分: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]];