Ios7 拟合边界未按预期工作

Ios7 拟合边界未按预期工作,ios7,xcode5,google-maps-sdk-ios,Ios7,Xcode5,Google Maps Sdk Ios,我将浏览这些页面,了解如何在给定边界上缩放视图并使其居中。中提供了这方面的代码,其中提到“有时移动相机以使整个感兴趣区域在最大可能的缩放级别下可见是有用的。” 这句话的意思是,“返回一个GMSCameraUpdate,它可以变换相机,使指定的边界以最大可能的缩放级别在屏幕上居中。” 下面的代码直接取自入门页面中的两个链接——稍加修改以提供有意义的屏幕截图;调整对实际结果没有影响 - (void)loadView { // Create a GMSCameraPosition that tells

我将浏览这些页面,了解如何在给定边界上缩放视图并使其居中。中提供了这方面的代码,其中提到“有时移动相机以使整个感兴趣区域在最大可能的缩放级别下可见是有用的。”

这句话的意思是,“返回一个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;
}
但是,预期结果与实际结果不符

- (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;
}
预期结果

实际结果


也许我对“最大可能的缩放级别”的含义感到困惑。我认为这意味着尽可能地放大,而不是缩小。不管怎样,我做错了什么,还是这是一个bug?

这里是对您的代码的一个轻微更改,使其按预期工作:

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

请参阅以获取解释。

嗨,Brett,在ViewDidDisplay中分配GMSMapView是个好主意吗?或者最好将地图创建保持在loadView中,但将相机更新推到ViewDidDisplay(或者ViewWillDisplay)?我曾想过这样做,但从未尝试过。我不知道谷歌为什么使用(void)loadView,但这是我可以在这个问题之外探讨的问题。谢谢你,布雷特!我们在哪里使用了loadView?我很高兴根据需要更新发布的示例代码=)@Brett:Getting Started doco使用loadView:谢谢提供详细信息!