Google Maps GMSMapView on custom UIView

后端 未结 8 1668
陌清茗
陌清茗 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:57

    I finally figured out how to display the map in a UIView created in Storyboard and center it in a specific location.

    Note: After created the UIView outlet, I changed its class to GMSMapView as explained before.

    import UIKit
    import GoogleMaps
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var searchBar: UISearchBar!
        @IBOutlet weak var toolBar: UIToolbar!
        @IBOutlet weak var mapView: GMSMapView!
    
        override func viewDidLoad() {
    
            super.viewDidLoad()
    
            let camera = GMSCameraPosition.camera(withLatitude: 52.520736, longitude: 13.409423, zoom: 12)
            self.mapView.camera = camera
    
            let initialLocation = CLLocationCoordinate2DMake(52.520736, 13.409423)
            let marker = GMSMarker(position: initialLocation)
            marker.title = "Berlin"
            marker.map = mapView
    
        }
    

    And this is the output: Map centred in Berlin

提交回复
热议问题