更改Google Map的大小iOS Swift [英] Change Google Map size iOS Swift

查看:65
本文介绍了更改Google Map的大小iOS Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此上找不到任何东西,所以我问.当我使用Google在其网站上提供的代码时,我在全屏控制器中获得了一张地图.我希望能够在容器中显示它(固定的宽度和高度).

I can't find anything on this so I am asking. when I use the code provided by Google on its website, I get a map in the controller that is full screen. I want to be able to show it within a container (fixed width & height).

我正在迅速使用,感谢您的帮助.以下是我从Google提取的代码:

I am using swift and would appreciate your help. Below is the code I extracted from google:

Override func viewDidLoad() {
        super.viewDidLoad()

        var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 2)
        var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true
        self.view = mapView

        var marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView

        // Do any additional setup after loading the view.
    }

推荐答案

您的地图将和您使用的视图一样大.您有2个选择.

Your map is going to be as big as the view you are using. You have 2 options.

  • 1
    创建特定大小的子视图,然后以与您相同的方式将该视图用于GMSMapView.
  • 2
    不必将GMSMapView分配给某些视图使用

  • 1
    Create subview of specific size and use that view for your GMSMapView, same way you do.
  • 2
    Instead of assigning your GMSMapView to some view use

view.insertSubview(mapView, atIndex:0)

请记住,在这种情况下,您必须为GMSMapView创建框架

Remember that you have to create a frame for your GMSMapView in this case

var mapView = GMSMapView.mapWithFrame(CGRectMake(...))

对此的好处是,您可以在地图上方添加其他组件(某些按钮,标签等),这是第一种方法无法实现的.

Good thing about this is you can add other components above your map if you want to(some buttons, labels, etc.), thats not possible with first approach.

这是我使用自动布局的方法.我在情节提要板中创建了一些视图,并对其施加了约束,以便根据屏幕尺寸调整大小. 然后,初始化地图视图,并使用自动调整大小视图的大小,然后将mapView插入此视图.

This is the way I do it using autolayout. I created some view in storyboard, gave it constraints so it would resize according to screen size. Then I initialize my map view and use size of my autoresizing view and insert my mapView to this view.

mapView_ = [[GMSMapView alloc] initWithFrame:self.viewForMap.bounds];
mapView_.delegate = self;
mapView_.camera = camera;
[self.viewForMap insertSubview:mapView_ atIndex:0];

是的,我知道这是Objective-C,但这只是一个例子.

Yes I know this is Objective-C, but this is just an example.

这篇关于更改Google Map的大小iOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆