Lithium
Lithium

Reputation: 599

GMSMarker not appearing on map

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

Upvotes: 0

Views: 3339

Answers (1)

Lithium
Lithium

Reputation: 599

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

Upvotes: 2

Related Questions