0

以下は、マップビューをロードするために使用したコードです。

- (void)getLatLongCoordinates:(NSString*) addressStr firstNameTitle:(NSString*) firstNamesTitle lastNameTitle:(NSString*) lastNamesTitle       { 
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
[geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
    self.placemarksArray = placemarks;
    CLPlacemark *placeInfo = [placemarks objectAtIndex:0];

    mapCenter.latitude = placeInfo.location.coordinate.latitude;
    mapCenter.longitude = placeInfo.location.coordinate.latitude;
    [annotationPoint setCoordinate:mapCenter];

    [annotationPointsArray addObject:annotationPoint];

    addAnnotation = [[[MyAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstNamesTitle SubTitle:lastNamesTitle ]autorelease];
    addAnnotation.firstNameTitle = firstNamesTitle;
    addAnnotation.lastNameTitle = lastNamesTitle;

[mapView addAnnotation:addAnnotation];
}];

}

メソッドを呼び出して、[self zoomToFitMapAnnotations:mapView withArray:annotationPointsArray]; すべての連絡先をマップビューに最大ズーム レベルで表示する必要があります。以前の実装まで、私は次のコードをうまく使用してきました。しかし、現在ブロックを使用しているため、このメソッドをいつ呼び出すかについて少し混乱しています。このメソッドを呼び出して配列を渡す前に、すべての位置座標を取得する必要があります。

-(void)zoomToFitMapAnnotations:(MKMapView*)mapViews withArray:(NSArray*)anAnnotationArray
{
if([mapViews.annotations count] == 0) return;

CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;

CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;



for(MKPointAnnotation* annotation in anAnnotationArray)
{
    topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
    topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

    bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
    bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}

MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;     
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; 

region = [mapViews regionThatFits:region];
[mapView setRegion:region animated:NO];
}

上記のメソッドを呼び出すための上記のコード部分の正しい場所はどこですか、それともこれを行うには他の方法を見つける必要がありますか。

編集:メソッドの使用を避け、メソッドmapCenterから直接座標にアクセスしましたが、正しく取得でき、問題は解決しました。しかし、なぜ最初の方法がうまくいかなかったのか、私はまだ困惑しています。placeInfogetLatLongCoordinates

4

2 に答える 2

0

私の場合、この方法を使用します

通常、マップビューに注釈を追加した後にこのメソッドを呼び出します。したがって、annotationView を mapVIEW に追加した後にこのメソッドを呼び出すのが最善の方法です。

于 2013-03-21T05:49:14.633 に答える
0

このコードを使用してください zoomToFitMapAnnotations

-(void)zoomToFitMapAnnotations:(MKMapView*)mapView_
{
    if([mapView_.annotations count] == 0)
        return;

    CLLocationCoordinate2D topLeftCoord;
    topLeftCoord.latitude = -90;
    topLeftCoord.longitude = 180;

    CLLocationCoordinate2D bottomRightCoord;
    bottomRightCoord.latitude = 90;
    bottomRightCoord.longitude = -180;

    for(CKMapAnnotationView* annotation in mapView_.annotations)
    {
        topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
        topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

        bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
        bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
    }
    MKCoordinateRegion region;
    region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
    region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
    region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;

    region = [mapView_ regionThatFits:region];


    [mapView_ setRegion:[self validRegion:region] animated:YES];
}

編集:注釈をロードした後にこの関数を呼び出す

[self zoomToFitMapAnnotations:mapView];
于 2013-03-21T05:55:17.040 に答える