【发布时间】:2010-10-29 14:25:03
【问题描述】:
我正在寻求一些帮助,以根据当前位置注释和我设置的注释完成一些关于在 MKMapView 上设置区域的代码。
我想计算两者之间的距离并设置两者之间的中心,然后缩小以使两者都在视图中。对我来说,它在模拟器中似乎工作正常,但不幸的是 userLocation.coordinate 固定在 Apple HQ 上。当我在设备上进行测试时,我看到了奇怪的行为。如果两个注释在同一纬度上有点水平,它通常会缩小并设置适当的区域,但如果垂直距离较大,则不能正确缩小。
我使用了找到 here 的代码,并进行了一些编辑以满足我的需要:
CLLocationCoordinate2D southWest = mapView.userLocation.coordinate;
CLLocationCoordinate2D northEast = southWest;
southWest.latitude = MIN(southWest.latitude, annotation.coordinate.latitude);
southWest.longitude = MIN(southWest.longitude, annotation.coordinate.longitude);
northEast.latitude = MAX(northEast.latitude, annotation.coordinate.latitude);
northEast.longitude = MAX(northEast.longitude, annotation.coordinate.longitude);
CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
// This is a diag distance (if you wanted tighter you could do NE-NW or NE-SE)
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 = 0.0;
MKCoordinateRegion savedRegion = [mapView regionThatFits:region];
[mapView setRegion:savedRegion animated:YES];
[locSouthWest release];
[locNorthEast release];
让我困惑的一件事是他说northEast = southWest...
提前感谢任何获得帮助和意见的人:)
【问题讨论】:
标签: objective-c mkmapview mkannotation