Search code examples
swift3xcode8

displaying google map within a UIView


I am using google maps with iOS.

this is my code:

class ViewController: UIViewController {
    @IBOutlet weak var myMapView: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Create a GMSCameraPosition that tells the map to display the
        // coordinate -33.86,151.20 at zoom level 6.
        let camera = GMSCameraPosition.camera(withLatitude: +31.75097946, longitude: +35.23694368, zoom: 17.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    mapView.isMyLocationEnabled = true
    mapView.mapType =  .terrain
    self.view = mapView
    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: +31.75097946, longitude: +35.23694368)
    marker.title = "my location"
    marker.map = mapView
}

How would i get the map to be attached to myMapView UIView? Is there a way for the marker title to appear always?

thanks


Solution

  • You just have to use the outlet you created.

    class ViewController: UIViewController {
        @IBOutlet weak var myMapView: GMSMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Create a GMSCameraPosition that tells the map to display the
            // coordinate -33.86,151.20 at zoom level 6.
            let camera = GMSCameraPosition.camera(withLatitude: +31.75097946, longitude: +35.23694368, zoom: 17.0)
            let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
            mapView.isMyLocationEnabled = true
            mapView.mapType =  .terrain
    
            // CHANGE THIS
            self.myMapView = mapView
    
            // Creates a marker in the center of the map.
            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2D(latitude: +31.75097946, longitude: +35.23694368)
            marker.title = "my location"
            marker.map = mapView
        }