2013-10-24 74 views
9

我正在瀏覽Google Maps SDK for iOS Getting Started頁面,以瞭解如何縮放視圖並將其放在給定邊界上。該代碼在Build a GMSCameraPosition中提供,其中提到「移動相機有時非常有用,以便在儘可能最大的縮放級別上顯示整個感興趣的區域。」擬合邊界不按預期工作

該措辭是similar to another possible approach via a GMSCameraUpdate,「返回一個GMSCameraUpdate,它轉換攝像頭,使指定邊界以儘可能最大的縮放級別居中在屏幕上。」

下面的代碼是從入門頁面的兩個鏈接中直接獲取的 - 稍作修改以提供有意義的屏幕截圖;適應對實際結果沒有影響。

- (void)loadView 
{ 
// Create a GMSCameraPosition that tells the map to display the 
// coordinate -33.86,151.20 at zoom level 6. 
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 
                 longitude:151.20 
                  zoom:6]; 
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
mapView_.myLocationEnabled = YES; 
self.view = mapView_; 

CLLocationCoordinate2D vancouver = CLLocationCoordinate2DMake(49.26, -123.11); 
CLLocationCoordinate2D calgary = CLLocationCoordinate2DMake(51.05, -114.05); 

GMSMarker *vancouverMarker = [[GMSMarker alloc] init]; 
vancouverMarker.position = vancouver; 
vancouverMarker.title = @"Vancouver"; 
vancouverMarker.map = mapView_; 

GMSMarker *calgaryMarker = [[GMSMarker alloc] init]; 
calgaryMarker.position = calgary; 
calgaryMarker.title = @"Calgary"; 
calgaryMarker.map = mapView_; 

GMSCoordinateBounds *bounds = 
[[GMSCoordinateBounds alloc] initWithCoordinate:vancouver coordinate:calgary]; 

[mapView_ moveCamera:[GMSCameraUpdate fitBounds:bounds]]; 
//These last two lines are expected to give the same result as the above line 
//camera = [mapView_ cameraForBounds:bounds insets:UIEdgeInsetsZero]; 
//mapView_.camera = camera; 
} 

但是,預期結果與實際結果不符。

預期結果
expected

實際結果
actual

也許我感到困惑的意思是 「最大可能的縮放級別。」我認爲這意味着儘量縮小,而不是縮小。無論哪種方式,我做錯了什麼或這是一個錯誤?

回答

21

這裏是你的代碼的微小變化,使得它按預期工作:

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    // Create a GMSCameraPosition that tells the map to display the 
    // coordinate -33.86,151.20 at zoom level 6. 
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 
                 longitude:151.20 
                  zoom:6]; 
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
    mapView_.myLocationEnabled = YES; 
    self.view = mapView_; 

    CLLocationCoordinate2D vancouver = CLLocationCoordinate2DMake(49.26, -123.11); 
    CLLocationCoordinate2D calgary = CLLocationCoordinate2DMake(51.05, -114.05); 

    GMSMarker *vancouverMarker = [[GMSMarker alloc] init]; 
    vancouverMarker.position = vancouver; 
    vancouverMarker.title = @"Vancouver"; 
    vancouverMarker.map = mapView_; 

    GMSMarker *calgaryMarker = [[GMSMarker alloc] init]; 
    calgaryMarker.position = calgary; 
    calgaryMarker.title = @"Calgary"; 
    calgaryMarker.map = mapView_; 

    GMSCoordinateBounds *bounds = 
    [[GMSCoordinateBounds alloc] initWithCoordinate:vancouver coordinate:calgary]; 

    [mapView_ moveCamera:[GMSCameraUpdate fitBounds:bounds]]; 
    //These last two lines are expected to give the same result as the above line 
    //camera = [mapView_ cameraForBounds:bounds insets:UIEdgeInsetsZero]; 
    //mapView_.camera = camera; 
} 

一個解釋見Difference between viewDidLoad and viewDidAppear

+0

嗨佈雷特,在viewDidAppear中分配GMSMapView是個好主意嗎?或者,最好在loadView中保留地圖創建,但將相機更新推到viewDidAppear(或者viewWillAppear)? –

+0

我想過這樣做,但從來沒有嘗試過。我不知道爲什麼谷歌使用(無效)loadView,但這是我可以看到這個問題以外的東西。謝謝,佈雷特! –

+0

我們在哪裏使用loadView?我很高興根據需要更新發布的示例代碼=) – Brett