【发布时间】:2010-11-12 22:01:19
【问题描述】:
我正在做的事情的快速背景。 UIMapView 加载并显示一个注释(从不超过一个)。在菜单栏上,有一个 Locate Me 按钮,点击后会找到 userLocation 并显示为第二个注释。然后我让 MapView 缩小以适应范围内的这两个注释,但我找不到合适的方法。根据第一个注释相对于用户位置的放置位置,有时它不会足够缩小。
例如,如果注释是用户的西北,它会很好地缩小。但是,如果注释是用户的东南方,它只会缩小到足以使它们被截断,您必须手动缩小一点。这是我正在使用的代码,是我在 SO 上找到的其他代码的变体。
CLLocation *whereIAm = mapView.userLocation.location;
float lat = whereIAm.coordinate.latitude;
float lon = whereIAm.coordinate.longitude;
CLLocationCoordinate2D southWest = {[currentLatitude floatValue], [currentLongitude floatValue]};
CLLocationCoordinate2D northEast = southWest;
southWest.latitude = MIN(southWest.latitude, lat);
southWest.longitude = MIN(southWest.longitude, lon);
northEast.latitude = MAX(northEast.latitude, lat);
northEast.longitude = MAX(northEast.longitude, lon);
CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast];
MKCoordinateRegion region;
region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;
region.span.latitudeDelta = meters / 111319.5
region.span.longitudeDelta = 7.0;
MKCoordinateRegion savedRegion = [mapView regionThatFits:region];
[mapView setRegion:savedRegion animated:YES];
[locSouthWest release];
[locNorthEast release];
是否有更好的方法内置到 MapKit 中来缩小以便两个注释都有,比如说在外框它们之间有半英寸?我真的不在乎用户是否必须在之后放大,我只是希望它能够正确缩小。
【问题讨论】:
标签: ios objective-c iphone cocoa-touch mapkit