自定义UIView上的Google Maps GMSMapView

Lin*_*rth 14 google-maps uiview ios swift

我想在添加到的视图上显示谷歌地图self.view,而不是直接在地图上绘制self.view.因此,我在故事板中创建了一个视图并将其类更改为GMSMapView.我还创建了一个名为的视图的插座连接gmView.

我使用以下代码,遗憾的是不显示地图:

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

另外,我尝试将mapViewto 添加self.view为子视图,如下所示:

self.view.addSubview(mapView)
Run Code Online (Sandbox Code Playgroud)

...并插入它:

self.view.insertSubview(mapView, at: 0)
Run Code Online (Sandbox Code Playgroud)

请注意,如果更改了任何内容,我将使用自动布局.

这些方法似乎都不适合我.
有任何想法吗?

Raj*_*ari 25

如果要在加载mapView后添加一个view,则需要创建一个对象GMSMapView.因此,打破你的出口,mapView因为它将动态创建.

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!)

    }
}
Run Code Online (Sandbox Code Playgroud)

这是输出.mapView宽度= 200,高度= 200,中心相同self.view

在此输入图像描述


cel*_*iux 25

我终于想出了如何在Storyboard中创建的UIView中显示地图并将其置于特定位置.

注意:创建UIView插座后,我将其类更改为GMSMapView,如前所述.

import UIKit
import GoogleMaps

class ViewController: UIViewController {

    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var toolBar: UIToolbar!
    @IBOutlet weak var mapView: GMSMapView!

    override func viewDidLoad() {

        super.viewDidLoad()

        let camera = GMSCameraPosition.camera(withLatitude: 52.520736, longitude: 13.409423, zoom: 12)
        self.mapView.camera = camera

        let initialLocation = CLLocationCoordinate2DMake(52.520736, 13.409423)
        let marker = GMSMarker(position: initialLocation)
        marker.title = "Berlin"
        marker.map = mapView

    }
Run Code Online (Sandbox Code Playgroud)

这是输出: 地图以柏林为中心


myl*_*mhz 6

您可以在布局中放置空白UIView,并在检查器中将其类设置为GMSMapView.您不需要以编程方式实例化它.

然后通过插座获取参考视图并设置坐标/摄像头.


Dev*_*ngh 6

非常简单.使用以下步骤

1)打开故事板:从ViewController中的Object库中删除UIView.

2)在班上制作自定义视图插座.(在我的代码中查看forGMap)

3)为您的班级中的代码添加以下行.

class GMapVC: UIViewController {

@IBOutlet weak var viewForGMap: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    let camera = GMSCameraPosition.camera(withLatitude: 28.7041, longitude: 77.1025, zoom: 10.0)
    let mapView = GMSMapView.map(withFrame: self.viewForGMap.frame, camera: camera)
    self.view.addSubview(mapView)

    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: 28.7041, longitude: 77.1025)
    marker.title = "Delhi"
    marker.snippet = "India’s capital"
    marker.map = mapView

   }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

输出:

在此输入图像描述