我正在寻找一些帮助来完成一些代码,以根据当前位置注释和我设置的注释在 MKMapView 上设置区域。
我想计算两者之间的距离并设置两者之间的中心,然后缩小以便两者都在视图中。对我来说,它在模拟器中似乎工作正常,但不幸的是 userLocation.coordinate 固定在 Apple HQ 上。当我在设备上进行测试时,我看到了奇怪的行为。如果两个注释在同一纬度上有点水平,它通常会缩小并设置适当的区域,但如果垂直距离更大,则不能正确缩小。
我使用了在这里找到的代码,并进行了一些编辑以满足我的需要:
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
...
提前感谢任何获得帮助和意见的人:)