以下は、マップビューをロードするために使用したコードです。
- (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
から直接座標にアクセスしましたが、正しく取得でき、問題は解決しました。しかし、なぜ最初の方法がうまくいかなかったのか、私はまだ困惑しています。placeInfo
getLatLongCoordinates