2012-10-04 111 views
0

我嘗試修復地圖中幾個位置的縮放和居中。這是我用和沒有工作的代碼:爲地圖中的某些位置設置縮放和中心

 MKCoordinateRegion region; 

     CLLocationDegrees maxLat = -90; 
     CLLocationDegrees maxLon = -180; 
     CLLocationDegrees minLat = 90; 
     CLLocationDegrees minLon = 180; 
     for(int idx = 0; idx < arrLocation.count; idx++)// here use your array or points 
     { 
      CLLocation* currentLocation = [arrLocation objectAtIndex:idx]; 
      if(currentLocation.coordinate.latitude > maxLat) 
       maxLat = currentLocation.coordinate.latitude; 
      if(currentLocation.coordinate.latitude < minLat) 
       minLat = currentLocation.coordinate.latitude; 
      if(currentLocation.coordinate.longitude > maxLon) 
       maxLon = currentLocation.coordinate.longitude; 
      if(currentLocation.coordinate.longitude < minLon) 
       minLon = currentLocation.coordinate.longitude; 
     } 

     region.center.latitude  = (maxLat + minLat)/2; 
     region.center.longitude = (maxLon + minLon)/2; 
     region.span.latitudeDelta = (maxLat - minLat) * 2; 
     region.span.longitudeDelta = (maxLon - minLon) * 2; 

     [mapView setRegion:region animated:YES]; 

我試圖在didAddAnnotationViewsdidUpdateUserLocation,但沒有奏效。你可以幫我嗎?

回答

0

終於讓我找到了以這種方式解決:

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

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

for(id<MKAnnotation> 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; 

// Add a little extra space on the sides 
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; 

region = [mapView regionThatFits:region]; 
[mapView setRegion:region animated:YES]; 
0

使用此代碼

MKMapRect flyTo = MKMapRectNull; 
    for (id <MKAnnotation> annotation in self.arrayAnnotation) 
    { 
     MyLog(@"fly to on"); 
     MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); 
     MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0); 
     if (MKMapRectIsNull(flyTo)) 
     { 
      flyTo = pointRect; 
     } 
     else 
     { 
      flyTo = MKMapRectUnion(flyTo, pointRect); 
      //MyLog(@"else-%@",annotationPoint.x); 
     } 
    } 

    // Position the map so that all overlays and annotations are visible on screen. 
    mapView.visibleMapRect = flyTo; 

希望它可以幫助你.....

+0

我應該在哪裏使用它?什麼是arrayAnnotation?你能否提供一些關於這種方法的信息。 – Ali

+0

arrayAnnotation是存儲所有註釋的地方,它可能在您的案例中有不同的名稱 –

0
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(theCoordinate, 1000, 1000); 
MKCoordinateRegion adjustedRegion = [pointOnMapView regionThatFits:viewRegion]; 
[pointOnMapView setRegion:adjustedRegion animated:NO]; 

pointOnMapView是我MKMapView

theCoordinate類型是CLLocationCoordinate2D

相關問題