2013-06-27 29 views
0

我找到了有關縮放的主題以適合標準尺寸方法的所有註釋。我使用的代碼,但問題是地圖只有150'的高度(但立場寬度)。我使用的代碼一切看起來很好。但問題是地圖的上邊緣上顯示的引腳總是不適合地圖區域縮放以適合非站點尺寸地圖的所有註釋

這裏是代碼。我試圖增加填充,但地圖仍然看起來不平衡。似乎我必須重新計算跨度的中心點。任何人都有這個非標準大小的地圖的解決方案?我注意到在foursquare應用程序中,它確實存在我面對的相同問題,因爲當地圖很小時,上邊緣顯示的引腳不適合。

在此先感謝

(void)zoomMapViewToFitAnnotations:(MKMapView *)mapView animated:(BOOL)animated 
{ 

NSArray *annotations = mapView.annotations; 
int count = [mapView.annotations count]; 
if (count == 0) { return; } //bail if no annotations 

//convert NSArray of id <MKAnnotation> into an MKCoordinateRegion that can be used to set the map size 
//can't use NSArray with MKMapPoint because MKMapPoint is not an id 
MKMapPoint points[count]; //C array of MKMapPoint struct 

for(int i=0; i<count; i++) //load points C array by converting coordinates to points 
{ 
    CLLocationCoordinate2D coordinate = [(id <MKAnnotation>)[annotations objectAtIndex:i] coordinate]; 
    points[i] = MKMapPointForCoordinate(coordinate); 
} 



//create MKMapRect from array of MKMapPoint 

MKMapRect mapRect = [[MKPolygon polygonWithPoints:points count:count] boundingMapRect]; 
//convert MKCoordinateRegion from MKMapRect 
MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect); 

//add padding so pins aren't scrunched on the edges 
    region.span.latitudeDelta *= 1.5*ANNOTATION_REGION_PAD_FACTOR; 
    region.span.longitudeDelta *= ANNOTATION_REGION_PAD_FACTOR; 
//but padding can't be bigger than the world 
    if(region.span.latitudeDelta > MAX_DEGREES_ARC) { region.span.latitudeDelta = MAX_DEGREES_ARC; } 
if(region.span.longitudeDelta > MAX_DEGREES_ARC){ region.span.longitudeDelta = MAX_DEGREES_ARC; } 

    //and don't zoom in stupid-close on small samples 
if(region.span.latitudeDelta < MINIMUM_ZOOM_ARC) { region.span.latitudeDelta = MINIMUM_ZOOM_ARC; } 
if(region.span.longitudeDelta < MINIMUM_ZOOM_ARC) { region.span.longitudeDelta = MINIMUM_ZOOM_ARC; } 
//and if there is a sample of 1 we want the max zoom-in instead of max zoom-out 
if(count == 1) 
{ 
    region.span.latitudeDelta = MINIMUM_ZOOM_ARC; 
    region.span.longitudeDelta = MINIMUM_ZOOM_ARC; 
} 
[mapView setRegion:region animated:animated]; 

}

回答

5

檢查了這一點,

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

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

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

    for (id <MKAnnotation> annotation in theMapView.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 = [theMapView regionThatFits:region]; 
    [theMapView setRegion:region animated:YES]; 
} 
+0

感謝您的答覆。但它仍然無法正常工作。我認爲問題是我的地圖有寬度>高度,但標準地圖有高度>寬度 –

+0

我們可以通過使用上述代碼設置最小縮放級別嗎?那麼請指導我, – iPatel

相關問題