0

In swift, I want to show a google-map with a marker on it. here's storyboard image: enter image description here

and here's the code

        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

in the line: 'self.map = mapView' if I write 'self.view = mapView' it will work fine and shows me the marker. but I have other views in the page along with mapView and 'self.view = mapView' will hide them. camera goes to the coordniates also. the only problem is that the marker is not appearing. what should I do? thanks

Lithium
  • 589
  • 2
  • 11
  • 24

1 Answers1

2

I solved the problem after hours googling. the key is to use Container View inside the mainView and in the containerView initializing the map with these codes:

 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
Lithium
  • 589
  • 2
  • 11
  • 24
  • as far as I remember, I put a container view on my view, and put the mapview inside the container view and used this code to resolve the problem. – Lithium Sep 04 '16 at 06:36