2010-09-08 72 views
0

alt textiPhone佈局區域怪誕

所附的圖像是當我嘗試尺寸MKMapView,以適應一個或多個地標是什麼有時會發生。它是間歇性的,但顯示屏始終處於完全相同的位置。

下面是代碼:

// loc1 is always non-null, and is equal to one of the annotation locations 

CLLocationCoordinate2D topLeftCoord = loc1.coordinate; 
CLLocationCoordinate2D bottomRightCoord = loc1.coordinate; 
for(UserPlacemark* annotation in self.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; 
double k = 0.01; 
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.25; 
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5; 
region.span.latitudeDelta = k + fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.25; // Add a little extra space on the sides 
region.span.longitudeDelta = k + fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.5; // Add a little extra space on the sides 

// only zoom if region doesn't fit, or too small 
CGRect newRect = [mapView convertRegion:region toRectToView:mapView]; 
double MIN_SIZE_PIXELS = 50.0; 
double rectSizePixels = newRect.size.width+newRect.size.height; 
if (!CGRectContainsRect(mapView.bounds, newRect) || rectSizePixels < MIN_SIZE_PIXELS) 
{ 
     region = [mapView regionThatFits:region]; 
     [mapView setRegion:region animated:TRUE]; 
} 

回答

0

在上面的代碼的問題是,一個MKUserAnnotation實例由地圖框架放置在地標的清單。這並不總是與用戶的實際位置一致,至少在模擬器中是如此。

最好避免迭代註釋列表,而是直接使用對象計算地標的最小/最大邊界。