2013-08-06 64 views
0

我在UIViewController中使用地圖視圖,我也提供了一個默認位置來放大,同時還有註釋。但是,縮放無法正常工作。註釋工作正常,但初始縮放到位置不起作用。iOS 6地圖沒有顯示提供的lat long位置

我不確定是什麼導致了這個問題。任何幫助將不勝感激。

這裏是我的代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    MKCoordinateRegion myRegion; 

    //Center 
    CLLocationCoordinate2D center; 
    center.latitude = 39.21516; 
    center.longitude = -76.580806; 

    //Span 
    MKCoordinateSpan span; 
    span.latitudeDelta = 0.02f; 
    span.longitudeDelta = 0.02f; 

    myRegion.center = center; 
    myRegion.span = span; 

    //set mapView 
    [self.mapView setRegion:ascRegion animated:YES]; 

    //Annotation 
    //Create coordinate for annotation 
    CLLocationCoordinate2D theLocation; 
    theLocation.latitude = 39.21516; 
    theLocation.longitude = -76.580806; 

    MyMapAnnotation * myAnnotation = [MyMapAnnotation alloc]; 
    myAnnotation.coordinate = theLocation; 
    myAnnotation.title = @"I am here"; 
    myAnnotation.subtitle = @"This is where I am"; 

    [self.mapView addAnnotation:myAnnotation]; 
} 

回答

1

嘗試按照this answer

- (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(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]; 
} 
+1

謝謝!這似乎是我需要做的,特別是因爲我的下一步是包含更多註釋,並且需要能夠查看它們!太棒了,感謝您的幫助! – shadonar

+0

@shadonar沒問題。幾周前我正在做類似的事情。 Upvote我連接的答案,以便人們可以輕鬆地看到它。 –