9

我正在浏览适用于 iOS 的 Google Maps SDK 入门页面,以了解如何在给定范围内缩放和居中视图。Build a GMSCameraPosition中提供了这方面的代码,其中提到“有时移动相机以使整个感兴趣区域在最大可能的缩放级别下可见是有用的。”

这个措辞类似于通过 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;
}

但是,预期结果与实际结果不符。

预期结果
预期的

实际结果
实际的

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

4

1 回答 1

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

有关说明,请参阅viewDidLoad 和 viewDidAppear 之间的区别

于 2013-10-25T00:16:11.530 回答