【发布时间】:2013-03-21 05:38:56
【问题描述】:
以下是我用来加载地图视图的代码。
- (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 并在getLatLongCoordinates 方法中直接从placeInfo 访问坐标,我做对了,问题就解决了。但是我仍然很困惑,为什么第一种方法不起作用。
【问题讨论】:
-
调用这个 [self zoomToFitMapAnnotations:mapView withArray:annotationPointsArray];在 [mapView addAnnotation:addAnnotation]; 之后
-
或者简单地调用这个方法 - (void)zoomToFitMapAnnotations { if ([mMapView.annotations count] == 0) return;诠释 i = 0; MKMapPoint points[[mMapView.annotations count]]; //为 (id
annotation in [mMapView annotations]){ points[i++] = MKMapPointForCoordinate(annotation.coordinate); 构建注释点数组} MKPolygon *poly = [MKPolygon polygonWithPoints:points count:i]; [mMapView setRegion:MKCoordinateRegionForMapRect([poly boundingMapRect]) 动画:YES]; }这将适合地图 -
目前你从哪里调用该方法,请用一些代码更新
-
就在
[mapView addAnnotation:addAnnotation];之后。问题是无论位置如何,地图视图都会缩放到中亚地区。 -
块执行完成后调用并检查
标签: iphone ios mkmapview mapkit clgeocoder