自定义UIView上的Google Maps GMSMapView [英] Google Maps GMSMapView on custom UIView

查看:131
本文介绍了自定义UIView上的Google Maps GMSMapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个视图上显示谷歌地图,然后添加到 self.view ,而不是直接在 self.view 。因此,我在storyboard中创建了一个视图,并将其类更改为 GMSMapView 。我还为该视图创建了一个名为 gmView 的出口连接。



我使用以下代码,但不会显示地图:

  let mapView = GMSMapView.map(withFrame:CGRect.zero,camera:GMSCameraPosition.camera(withLatitude:51.050657,longitude:10.649514,zoom:5.5))
gmView = mapView

另外,我尝试将 mapView 添加到 self。 view 作为子视图,如下所示:

  self.view.addSubview(mapView)

...并插入它:

  self.view.insertSubview(mapView,at:0)

请注意,我使用自动布局,如果这改变了任何东西。



这些方法都不适用于我。

任何想法?

解决方案

如果您想在加载<$ c $之后添加 mapView c> view ,那么你需要创建一个 GMSMapView 的对象。所以打破你的 mapView 的出口,因为它会动态创建。

 导入UIKit 
导入GoogleMaps

类MapViewController:UIViewController {

//带一个Google Map对象。不要从Storyboard中创建插件,如果你创建了插件
var mapView:GMSMapView,请打破GMSMapView的出口?

覆盖func viewDidLoad(){

super.viewDidLoad()

mapView = GMSMapView.map(withFrame:CGRect(x:100,y :100,width:200,height:200),camera:GMSCameraPosition.camera(withLatitude:51.050657,longitude:10.649514,zoom:5.5))

//所以mapView的宽度为200,height 200和它的中心是一样的self.view
mapView的中心?.center = self.view.center

self.view.addSubview(mapView!)



code
$ b

这里是输出。 mapView 的宽度= 200,高度= 200,中心与 self.view

$ b相同
$ b


I want to display google maps on a view that's then added to self.view, rather than drawing the map directly on self.view. Therefore, I created a view in storyboard and changed its class to GMSMapView. I also created an outlet connection to that view called gmView.

I am using the following code that does unfortunately not show the map:

let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: GMSCameraPosition.camera(withLatitude: 51.050657, longitude: 10.649514, zoom: 5.5))
gmView = mapView

Also, I tried adding the mapView to self.view as a subview, like this:

self.view.addSubview(mapView)

...and inserting it:

self.view.insertSubview(mapView, at: 0)

Note that I'm using Auto Layout if that changes anything.

None of these approaches seem to work for me.
Any ideas?

解决方案

If you want to add a mapView after the loading of the view, then you need to create an object of GMSMapView. So break the outlets of your mapView since it will be created dynamically.

import UIKit
import GoogleMaps

class MapViewController: UIViewController {

    //Take a Google Map Object. Don't make outlet from Storyboard, Break the outlet of GMSMapView if you made an outlet
    var mapView:GMSMapView?

    override func viewDidLoad() {

        super.viewDidLoad()

        mapView = GMSMapView.map(withFrame: CGRect(x: 100, y: 100, width: 200, height: 200), camera: GMSCameraPosition.camera(withLatitude: 51.050657, longitude: 10.649514, zoom: 5.5))

        //so the mapView is of width 200, height 200 and its center is same as center of the self.view
        mapView?.center = self.view.center

        self.view.addSubview(mapView!)

    }
}

Here is the output. mapView is of width = 200 and height = 200 with center as same as self.view

这篇关于自定义UIView上的Google Maps GMSMapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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