2013-10-24 72 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