0

我定义了两个函数:

mkgeometry_additions.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

MKMapRect MKMapRectForCoordinateRegion(MKCoordinateRegion region);

MKCoordinateRegion MKCoordinateRegionForSouthWestAndNorthEast(CLLocationCoordinate2D sw, CLLocationCoordinate2D ne);

mkgeometry_additions.m(.m,即使它们是 c 函数)

MKMapRect MKMapRectForCoordinateRegion(MKCoordinateRegion region)
{
    CLLocationCoordinate2D center = region.center;
    MKCoordinateSpan span = region.span;
    CLLocationCoordinate2D topLeft =
        CLLocationCoordinate2DMake(center.latitude - span.latitudeDelta / 2.0,
                                   center.longitude - span.longitudeDelta / 2.0);
    CLLocationCoordinate2D bottomRight =
    CLLocationCoordinate2DMake(center.latitude - span.latitudeDelta / 2.0,
                               center.longitude - span.longitudeDelta / 2.0);
    MKMapPoint mapPointTopLeft = MKMapPointForCoordinate(topLeft);
    MKMapPoint mapPointBottomRight = MKMapPointForCoordinate(bottomRight);
    double width = mapPointTopLeft.x - mapPointBottomRight.x;
    double height = mapPointTopLeft.y - mapPointBottomRight.y;

    return MKMapRectMake(mapPointTopLeft.x, mapPointTopLeft.y, width, height);
}

MKCoordinateRegion MKCoordinateRegionForSouthWestAndNorthEast(CLLocationCoordinate2D sw, CLLocationCoordinate2D ne)
{
    MKCoordinateSpan span =
        MKCoordinateSpanMake(ne.latitude - sw.latitude, ne.longitude - sw.longitude);
    CLLocationCoordinate2D topLeft = CLLocationCoordinate2DMake(ne.latitude, sw.longitude);
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(topLeft.latitude + span.latitudeDelta * 0.5, topLeft.longitude + span.longitudeDelta * 0.5);
    return MKCoordinateRegionMake(center, span);
}

这几乎是两个文件的全部内容。

在我使用 XCTests 进行的单元测试中:

- (void)testMKMapRectForCoordinateRegionAfterReverseConversion
{
    CLLocationCoordinate2D fremont = CLLocationCoordinate2DMake(37.54827, -121.98857);
    MKCoordinateRegion region = MKCoordinateRegionMake(fremont, MKCoordinateSpanMake(0.05, 0.05));
    MKMapRect mapRect = MKMapRectForCoordinateRegion(region);
    MKCoordinateRegion derivedRegion = MKCoordinateRegionForMapRect(mapRect);

    double accuracy = 0.0001;

    XCTAssertEqualWithAccuracy(region.center.latitude, derivedRegion.center.latitude,
                               accuracy, @"Latitude is equal");
    XCTAssertEqualWithAccuracy(region.center.longitude, derivedRegion.center.longitude,
                               accuracy, @"Latitude is equal");
    XCTAssertEqualWithAccuracy(region.span.latitudeDelta, derivedRegion.span.latitudeDelta,
                               accuracy, @"Latitude delta is equal");
    XCTAssertEqualWithAccuracy(region.span.longitudeDelta, derivedRegion.span.longitudeDelta,
                               accuracy, @"Latitude delta is equal");
}

- (void)testMKCoordinateRegionForSouthWestAndNorthEast
{
    CLLocationCoordinate2D taipeiWanHua = CLLocationCoordinate2DMake(25.02946, 121.49652);
    CLLocationCoordinate2D taipeiSongShan = CLLocationCoordinate2DMake(25.06640, 121.56166);

    /* When the following line is called, the first test fails */
    MKCoordinateRegionForSouthWestAndNorthEast(taipeiWanHua, taipeiSongShan);

   /* I have the rest of the lines in this test commented out */
}

现在,我有这个测试类的空设置和拆卸。当我运行测试时。如果第二个测试的第三行在测试中,则第一个测试失败。仔细观察会发现,当它失败时,这些值仍然相似,但超出了给定的精度: 在此处输入图像描述

如果我将第二个测试的第三行注释掉,第一个测试就会通过。这是为什么?

4

1 回答 1

0

问题不在于单元测试——对我来说,即使我完全注释掉第二个测试它也会失败——它与 MKMapRectForCoordinateRegion 实现有关。我认为正确的实现是:

MKMapRect MKMapRectForCoordinateRegion(MKCoordinateRegion region)
{
    CLLocationCoordinate2D center = region.center;
    MKCoordinateSpan span = region.span;
    CLLocationCoordinate2D topLeft =
    CLLocationCoordinate2DMake(center.latitude + span.latitudeDelta / 2.0,
                               center.longitude - span.longitudeDelta / 2.0);
    CLLocationCoordinate2D bottomRight =
    CLLocationCoordinate2DMake(center.latitude - span.latitudeDelta / 2.0,
                               center.longitude + span.longitudeDelta / 2.0);
    MKMapPoint mapPointTopLeft = MKMapPointForCoordinate(topLeft);
    MKMapPoint mapPointBottomRight = MKMapPointForCoordinate(bottomRight);
    double width = mapPointBottomRight.x - mapPointTopLeft.x;
    double height = mapPointBottomRight.y - mapPointTopLeft.y;

    MKMapRect ret = MKMapRectMake(mapPointTopLeft.x, mapPointTopLeft.y, width, height);
    return ret;
}

看看这个: 墨卡托投影

于 2013-11-04T08:37:34.553 回答