2010-11-12 29 views
14

Nền tảng nhanh về những gì tôi đang diễn ra. UIMapView tải và hiển thị một chú thích đơn (Không bao giờ nhiều hơn một chú thích). Trên thanh trình đơn, có một nút Xác định vị trí, trên tap userLocation được tìm thấy và hiển thị dưới dạng chú thích thứ hai. Tôi sau đó tôi có MapView thu nhỏ để phù hợp với cả hai chú thích trong phạm vi nhưng tôi không thể tìm thấy một cách thích hợp để làm như vậy. Tùy thuộc vào vị trí chú thích đầu tiên được đặt liên quan đến vị trí của người dùng, đôi khi nó không thu nhỏ đủ.Cách tốt nhất để thu nhỏ và phù hợp với tất cả chú thích trong MapKit

Ví dụ: nếu chú thích là Tây Bắc của Người dùng, nó sẽ thu nhỏ. Nhưng nếu chú thích là Đông Nam của Người dùng, nó chỉ thu nhỏ đủ để chúng bị cắt và bạn phải tự phóng to hơn một chút. Đây là mã tôi đang sử dụng, biến thể của một số người khác tôi đã tìm thấy trên 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]; 

Có cách nào tốt hơn được tích hợp vào MapKit để thu nhỏ để cả hai chú thích đều có cách nhau nửa inch giữa khung bên ngoài không? Tôi không thực sự quan tâm nếu người dùng phải phóng to sau, tôi chỉ muốn nó thu nhỏ một cách chính xác.

Trả lời

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]; 
} 

Trích từ: http://codisllc.com/blog/zoom-mkmapview-to-fit-annotations/

(. Sử dụng nó mọi lúc)

+0

Hmm Tôi đã thử cái này tại một thời điểm và nó không hoạt động như mong đợi. Tôi đã có kết quả tương tự. Tôi sẽ cung cấp cho nó một shot nhanh chóng mặc dù và nhìn thấy một lần nữa. Tôi sẽ báo cáo lại. –

+0

Ý của bạn là gì "như mong đợi?" Có vấn đề gì bạn đang có? –

+0

Chỉ cần có nó được thực hiện và nó hoạt động tốt hơn so với những gì tôi đã có vì vậy tôi sẽ gắn bó với nó. Điều duy nhất bị cắt bỏ bây giờ là chú thích mà tôi có thể giải quyết bằng cách làm cho nó trở lại sau khi hoạt hình. Cảm ơn bạn đã giúp đỡ. –

1

Những người làm MonoTouch C# mã hóa

BasicMapAnnotation là lớp kế thừa từ 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

Trong iOS7 có một phương thức thực hiện điều đó. Chỉ cần gọi:

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

Điều này thật tuyệt vời. Cảm ơn bạn đã hiển thị nó. Không linh động như chức năng tự tạo ra nhưng thực hiện công việc. Sẽ rất tuyệt nếu bạn có thể thêm côn trùng để có thêm không gian xung quanh các chú thích. – palme

+0

Ôi trời ... Điều này thật hoàn hảo ... Tôi đã sử dụng mã khoảng 30 dòng cho điều này và tôi đã bugging - điều này hoạt động hoàn hảo! –

1

Bạn có thể sử dụng mã này để hiển thị tất cả các chú thích

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]; 

nếu bạn muốn bao gồm vị trí người sử dụng chỉ cần thay thế hai dòng dưới đây với các dòng đầu tiên của mã trên

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

ngoài: 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 hoạt ảnh: YES]; –

Các vấn đề liên quan