适合边界不能按预期工作

时间:2013-10-24 20:35:44

标签: ios7 xcode5 google-maps-sdk-ios

我浏览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

也许我对"最大可能的缩放级别的含义感到困惑。"我认为它意味着尽可能紧密地放大,而不是缩小。无论哪种方式,我做错了什么或这是一个错误?

1 个答案:

答案 0 :(得分: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以获取解释。