【问题标题】:how to auto layout Google maps in Ios如何在 iOS 中自动布局谷歌地图
【发布时间】:2014-01-29 23:21:58
【问题描述】:

我是 ios 新手,正在尝试将地图嵌入到我的应用程序中。我不希望地图覆盖整个页面,而是寻找基于 ipad 旋转自动布局的方法。

GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

谷歌只举个例子,用绘制矩形来显示地图?我可以以其他方式显示地图,同时保留自动布局吗?

【问题讨论】:

  • 向地图视图添加约束,它应该像任何其他视图一样工作。
  • 不可能在 mapView 中应用约束,因为它是在代码中创建的,而不是由 InterfaceBuild。我也在寻找这个答案。 @LeonNatan
  • @TiagoAmaral 可以通过代码应用自动布局约束。无需使用界面生成器。

标签: ios7 google-maps-sdk-ios


【解决方案1】:

就在今天实现了每个人都可能需要的相同的东西:
使用PureLayout library,像普通UIView 一样布局GMSMapView 非常容易。

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.delegate = self;
// This is important when we layout view with constraints
mapView_.translatesAutoresizingMaskIntoConstraints = NO;
mapView_.myLocationEnabled = YES;
[self.view addSubview:mapView_];

// Size to fit the screen with zero insets
[mapView_ autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];

您将在屏幕上获得全屏地图视图。当您旋转屏幕时,它也可以工作。

【讨论】:

    【解决方案2】:

    创建一个矩形并将其传递给[GMSMapView mapWithFrame:frame camera:camera];

    以下片段对我有用。希望它对你有用。我创建了一个 UIVIEW,并在其上嵌入了地图。

    CGRect mapRect = CGRectMake(0, 0, self.mapView.frame.size.width,
                                  self.mapView.frame.size.height);
    mapView_ = [GMSMapView mapWithFrame:mapRect camera:camera];
    

    【讨论】:

      【解决方案3】:

      我使用FLKAutoLayout 从代码中添加约束。

      在 Interface Builder 中添加容器视图并添加约束以适应整个屏幕。 在您的 UIViewController 中,创建您的 GMSMapView 并添加这些行

      container.addSubview(mapView)
      
      mapView.constrainWidthToView(container, predicate: nil)
      mapView.constrainHeightToView(container, predicate: nil)
      

      这是 Swift 代码,但它也适用于 Objective-C。

      现在 GMSMapView 适合带有旋转支持的容器视图

      【讨论】:

        【解决方案4】:

        您可以通过界面生成器湖添加它任何其他自定义视图并在那里应用所需的约束:

        1. ViewController 内的任意位置放置一个UIView 对象
        2. 照常应用任何所需的约束
        3. 在界面构建器的Identity Inspector 选项卡下设置UIViewGMSMapView(就像您对任何其他自定义视图所做的那样)
        4. GMSMapView 连接为IBOutlet 与您的ViewController
        5. GMSMapView 实例提供任何进一步的自定义,以获得所需的行为。

        我实际上担心是否可以在不调用的情况下初始化 GMSMapView

        [GMSMapView mapWithFrame:CGRectZero camera:camera];
        

        因为这是 google 快速启动页面显示的示例。在像这样创建 GMSMapView 实例之后设置相机(如果您的应用程序需要)似乎是绝对没问题的:

        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:10];
        self.googleMapView.camera = camera;
        

        *self.googleMapView 是上述步骤中的 IBOutlet。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多