現在の場所の注釈と設定した注釈に基づいて、MKMapViewで領域を設定するためのコードを完成させるためのヘルプを探しています。
2つの間の距離を計算し、2つの間の中心を設定してから、両方が表示されるようにズームアウトします。私にとってはSimulatorで正常に動作しているように見えますが、残念ながらuserLocation.coordinateはAppleHQに固定されています。デバイスでテストすると、奇妙な動作が見られます。多くの場合、2つの注釈が同じ緯度で水平方向にいくらかある場合はズームアウトして適切な領域を設定しますが、垂直方向の距離が大きい場合は適切にズームアウトしません。
ここにあるコードを使用し、ニーズに合わせて少し編集しました。
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
です...
助けと入力を持っている人に事前に感謝します:)