Google Maps GMSMapView on custom UIView

后端 未结 8 1674
陌清茗
陌清茗 2020-12-14 20:33

I want to display google maps on a view that\'s then added to self.view, rather than drawing the map directly on self.view. Therefore, I created a

8条回答
  •  暖寄归人
    2020-12-14 20:59

    Here's a very simple way to get a Google Maps hooked up to a View in your ViewController in a storyboard (Swift 4.2, Xcode 10).

    I'm calling my ViewController MapLocationsViewController.

    1. So, in your storyboard, make sure the class on your ViewController is properly referencing your correct class (MapLocationsViewController for me in this example).

    2. Next, drag a View object onto that ViewController and set that class to GMSMapView.

    3. Hook that View object up to an @IBOutlet in your code (which I named viewForGoogleMap in my example).

    Here's a minimalistic example code:

    import UIKit
    import GoogleMaps
    class MapLocationsViewController: UIViewController {
        @IBOutlet weak var viewForGoogleMap: GMSMapView!
        override func viewDidLoad() {
            super.viewDidLoad()
            let mapView = GMSMapView(frame: self.view.bounds)
            viewForGoogleMap.addSubview(mapView)
        }
    }
    

    For further info, check out the Google Maps docs.

提交回复
热议问题