Google Maps GMSMapView on custom UIView

后端 未结 8 1672
陌清茗
陌清茗 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 21:03

    Very Simple. Use Following Steps

    1) Open Storyboard: Drop a UIView from Object library in your ViewController.

    2) Make custom view outlet in your class. (viewForGMap in my code)

    3) Add following line for code in your class.

    class GMapVC: UIViewController {
    
    @IBOutlet weak var viewForGMap: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Do any additional setup after loading the view.
    
        let camera = GMSCameraPosition.camera(withLatitude: 28.7041, longitude: 77.1025, zoom: 10.0)
        let mapView = GMSMapView.map(withFrame: self.viewForGMap.frame, camera: camera)
        self.view.addSubview(mapView)
    
        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: 28.7041, longitude: 77.1025)
        marker.title = "Delhi"
        marker.snippet = "India’s capital"
        marker.map = mapView
    
       }
    

    OutPut:

提交回复
热议问题