为了设置对外部服务器的查询,我想在我正在构建的 iPhone 应用程序中获取当前地图视图的边界。UIView 应该响应边界,但似乎 MKMapView 没有。设置区域并放大地图后,我尝试获取边界。我被困在第一步,即尝试获取代表地图 SE 和 NW 角的 CGPoints。之后我打算使用:
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view
将点转换为地图坐标。但我什至不能走那么远...
//Recenter and zoom map in on search location
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = mySearchLocation.searchLocation.coordinate;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[self.mapView setRegion:region animated:YES];
//After the new search location has been added to the map, and the map zoomed, we need to update the search bounds
//First we need to calculate the corners of the map
CGPoint se = CGPointMake(self.mapView.bounds.origin.x, mapView.bounds.origin.y);
CGPoint nw = CGPointMake((self.mapView.bounds.origin.x + mapView.bounds.size.width), (mapView.bounds.origin.y + mapView.bounds.size.height));
NSLog(@"points are: se %@, nw %@", se, nw);
代码编译时没有警告但是 se 和 nw 都是空的。查看 self.mapView.bounds.origin.x 变量为 0。尝试 NSLog 直接 self.mapView.bounds.size.width 给我一个“程序接收信号:“EXC_BAD_ACCESS”。这似乎来自 NSLog。
任何人都知道从 MKMapView 的可见区域获取东南角和西北角(在地图坐标中)的正确方法吗?
编辑:似乎每当您在这里提出问题时,答案就会立即出现。我使用 %@ 而不是 @f 来打印 NSLog 中的每个变量,这些变量在那里引发错误。我还发现了 MKMapview 的 annotationVisibleRect 属性。似乎 annotationVisibleRect 是基于父视图坐标的。