我定义了两个函数:
在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 */
}
现在,我有这个测试类的空设置和拆卸。当我运行测试时。如果第二个测试的第三行在测试中,则第一个测试失败。仔细观察会发现,当它失败时,这些值仍然相似,但超出了给定的精度:
如果我将第二个测试的第三行注释掉,第一个测试就会通过。这是为什么?