我正在使用 MKMapKit 并将该区域设置为全尺寸的美国,并且它按预期显示。但是,在检查该self.mapView.region
属性时,我得到了一些奇怪的结果,这些结果会影响我稍后在程序中的计算。
这是我设置地图视图的方式:
- (void)viewDidLoad
{
[super viewDidLoad];
MKCoordinateRegion usa = MKCoordinateRegionMake(CLLocationCoordinate2DMake(39.4, -91.5), MKCoordinateSpanMake(40, 40));
self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.mapView];
self.mapView.region = usa;
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
[self.mapView.userLocation addObserver:self forKeyPath:@"location" options:0 context:nil];
}
这是美国MKCoordinateRegion
,看起来不错并且可以工作:
(lldb) p usa
(MKCoordinateRegion) $0 = {
(CLLocationCoordinate2D) center = {
(CLLocationDegrees) latitude = 39.4
(CLLocationDegrees) longitude = -91.5
}
(MKCoordinateSpan) span = {
(CLLocationDegrees) latitudeDelta = 40
(CLLocationDegrees) longitudeDelta = 40
}
}
然后我设置区域self.mapView.region = usa;
并在它之后中断,这现在是self.mapView.region
.
(lldb) p [self.mapView region]
(MKCoordinateRegion) $1 = {
(CLLocationCoordinate2D) center = {
(CLLocationDegrees) latitude = 39.4
(CLLocationDegrees) longitude = 39.4
(CLLocationDegrees) latitudeDelta = 39.4
(CLLocationDegrees) longitudeDelta = -91.5
}
(MKCoordinateSpan) span = {
(CLLocationDegrees) latitude = 66.4999527377778
(CLLocationDegrees) longitude = 66.4999527377778
(CLLocationDegrees) latitudeDelta = 66.4999527377778
(CLLocationDegrees) longitudeDelta = 67.4999957172512
}
(CLLocationCoordinate2D) center = {}
}
所以问题是中心结构是错误的,经度是关闭的,CLLocationCoordinate2D
甚至不应该有增量字段,它看起来像一个MKCoordinateSpan
.
CLLocationCoordinate2D 定义:
typedef struct {
CLLocationDegrees latitude;
CLLocationDegrees longitude;
} CLLocationCoordinate2D;
所以知道我哪里出错了吗?我错过了一些非常明显的东西吗?如果这是错误的方法,我怎样才能获得真实数据以便稍后在应用程序中进行一些计算?
使用 xcode 4.6.1 和 iOS 6.1 模拟器。