GMSMarker没有出现在地图上

时间:2016-04-26 08:11:36

标签: swift google-maps

在swift中,我想显示一个带有标记的谷歌地图。 这是故事板图片: enter image description here

这是代码

        var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 6)

        var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true

        self.map.camera = camera
        self.map = mapView

        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
        marker.title = "title"
        marker.snippet = "snippet"
        marker.map = mapView
行中的

:'self.map = mapView'如果我写'self.view = mapView'它会正常工作并向我显示标记。但我在页面中有其他视图以及mapView和'self.view = mapView'将隐藏它们。相机也去了coordniates。唯一的问题是标记没有出现。 我该怎么办?谢谢

1 个答案:

答案 0 :(得分:2)

我在谷歌搜索后解决了这个问题。关键是在mainView中使用容器视图,并在containerView中使用以下代码初始化地图:

 var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 6)

        var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true

        self.map.camera = camera
        self.view = mapView

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