在 UIView 中显示 google 地图

displaying google map within a UIView

我正在使用 google 地图和 iOS。

这是我的代码:

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
}

如何将地图附加到 myMapView UIView? 有没有办法让标记标题一直出现?

谢谢

您只需使用您创建的插座即可。

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
    }

要在 UIView 中放置一个 google 地图,那么您需要将一个 UIView 对象拖到您的 canvas 上并将其子类化为 GMSMapView,然后在您的代码中为它创建一个出口 - 然后您可以使用以下代码对其进行初始化:

   @IBOutlet weak var miniview: GMSMapView!

   override func viewDidLoad() {
        super.viewDidLoad()
        let camera = GMSCameraPosition.camera(withLatitude: +31.75097946, longitude: +35.23694368, zoom: 6.0)
        miniview.camera = camera
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: +31.75097946, longitude: +35.23694368)
        marker.title = "my location"
        marker.map = miniview
    }