2010-11-12 19 views
14

快速瞭解我正在進行的操作。 UIMapView加載並顯示單個註釋(從不超過一個)。在菜單欄上,有一個「我的我」按鈕,點擊時找到userLocation並顯示爲第二個註釋。然後,我將MapView縮小以適應範圍內的這些註釋,但我無法找到合適的方法。根據第一個註釋與用戶位置相關的位置,有時它不會縮小到足夠大。什麼是縮小和適合MapKit中所有註釋的最佳方式

例如,如果註釋是用戶的西北部,則會放大。但是,如果註釋位於用戶的東南方,則它只能縮小到足以讓它們被切斷,而且必須手動縮小一點。這是我正在使用的代碼,我在SO上找到的其他一些代碼的變體。

 CLLocation *whereIAm = mapView.userLocation.location; 

     float lat = whereIAm.coordinate.latitude; 
     float lon = whereIAm.coordinate.longitude; 


     CLLocationCoordinate2D southWest = {[currentLatitude floatValue], [currentLongitude floatValue]}; 
     CLLocationCoordinate2D northEast = southWest; 

     southWest.latitude = MIN(southWest.latitude, lat); 
     southWest.longitude = MIN(southWest.longitude, lon); 

     northEast.latitude = MAX(northEast.latitude, lat); 
     northEast.longitude = MAX(northEast.longitude, lon); 

     CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude]; 
     CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude]; 

     CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast]; 

     MKCoordinateRegion region; 
     region.center.latitude = (southWest.latitude + northEast.latitude)/2.0; 
     region.center.longitude = (southWest.longitude + northEast.longitude)/2.0; 
     region.span.latitudeDelta = meters/111319.5 
     region.span.longitudeDelta = 7.0; 

     MKCoordinateRegion savedRegion = [mapView regionThatFits:region]; 
     [mapView setRegion:savedRegion animated:YES]; 

     [locSouthWest release]; 
     [locNorthEast release]; 

有內置MapKit一個更好的辦法只有縮小,這樣既註解有,可以說它們之間的半英寸的外框?我並不在乎用戶是否需要放大,我只是想讓它縮小。

回答

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

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

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

    for(MapAnnotation* 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; // Add a little extra space on the sides 

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

來自http://codisllc.com/blog/zoom-mkmapview-to-fit-annotations/

(使用這一切的時候)

+0

嗯,我確實嘗試過這一點,並沒有像預期的那樣工作。我有同樣的結果。儘管如此,我會盡快給它一次,然後再看一次。我會回報。 – 2010-11-12 22:08:51

+0

「意料之中」是什麼意思?你有什麼問題? – 2010-11-12 22:20:57

+0

剛剛實施,它比我所做的更好,所以我會堅持下去。現在唯一被切斷的是標註,我可以通過在動畫之後進行回放來解決這個問題。謝謝你的幫助。 – 2010-11-12 22:23:52

1

誰做MonoTouch的C#編碼

BasicMapAnnotation是MKAnnotation

繼承類
private void GetRegion(MKMapView mapView) 
{ 
    var userWasVisible = mapView.ShowsUserLocation; 
    mapView.ShowsUserLocation = false; // ignoring the blue blip 
    // start with the widest possible viewport 
    var tl = new CLLocationCoordinate2D(-90, 180); // top left 
    var br = new CLLocationCoordinate2D(90, -180); // bottom right 
    foreach (var an in mapView.Annotations) 
    { 
     // narrow the viewport bit-by-bit 
     CLLocationCoordinate2D coordinate = ((BasicMapAnnotation) an).Coordinate; 
     tl.Longitude = Math.Min(tl.Longitude, coordinate.Longitude); 
     tl.Latitude = Math.Max(tl.Latitude, coordinate.Latitude); 
     br.Longitude = Math.Max(br.Longitude, coordinate.Longitude); 
     br.Latitude = Math.Min(br.Latitude, coordinate.Latitude); 
    } 
    var center = new CLLocationCoordinate2D 
    { 
     // divide the range by two to get the center 
     Latitude = tl.Latitude - (tl.Latitude - br.Latitude)*0.5,Longitude = tl.Longitude + (br.Longitude - tl.Longitude)*0.5 

    }; 
    var span = new MKCoordinateSpan 
    { 
     // calculate the span, with 20% margin so pins aren’t on the edge 
     LatitudeDelta = Math.Abs(tl.Latitude - br.Latitude)*1.2,LongitudeDelta = Math.Abs(br.Longitude - tl.Longitude)*1.2 

    }; 
    var region = new MKCoordinateRegion {Center = center, Span = span}; 
    region = mapView.RegionThatFits(region); // adjusts zoom level too 
       mapView.SetRegion(region, true); // animated transition 
       mapView.ShowsUserLocation = 

    userWasVisible; 

} 
8

在iOS7中有一種方法可以做到這一點。只要致電:

[yourMapView showAnnotations:yourAnnotationsArray animated:YES]; 
+0

這太棒了。感謝您的展示。不像自制的功能那麼靈活,但它的工作。如果您可以添加昆蟲以在註釋周圍留出更多空間,會很酷。 – palme 2014-05-12 20:33:57

+0

噢,我的...這是完美的......我一直在使用30行左右的代碼,因此我一直在竊聽 - 這完美的工作! – 2015-04-14 19:18:20

1

您可以使用此代碼來顯示所有批註

MKMapRect zoomRect = MKMapRectNull; 
for (id <MKAnnotation> annotation in mapView.annotations) 
{ 
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); 
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); 
    zoomRect = MKMapRectUnion(zoomRect, pointRect); 
} 
[mapView setVisibleMapRect:zoomRect animated:YES]; 

如果要包括用戶的位置,只需更換下面的上面第一行代碼的兩行

MKMapPoint annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate); 
MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); 
+0

此外:double dx = zoomRect.size.width * CGRectGetWidth(self.map.bounds)/ CGRectGetWidth(self.view.bounds); double dy = zoomRect.size.height * CGRectGetHeight(self.map.bounds)/ CGRectGetHeight(self.view.bounds); zoomRect = MKMapRectInset(zoomRect,-dx/2,-dy/2); [self.map setVisibleMapRect:zoomRect animated:YES]; – 2015-07-27 13:40:21

相關問題