我在iOS上使用Google Maps。

这是我的代码:

import UIKit
import GoogleMaps

ViewController: UIViewController {

@IBOutlet weak var myMapView: GMSMapView!

override func viewDidLoad() {
    super.viewDidLoad()
}


override func viewDidAppear(_ animated: Bool) {
    // 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
    //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 = "Eden VidanPeled"

    //marker.snippet = "Australia"
    marker.map = mapView
    marker.opacity = 1.0
   }


在Interface Builder中,我有一个带有GMSMapView类的UIView。当我将地图附加到UIViewController时,我得到了经纬度坐标。当我尝试将地图吸引到UIView时,会得到不同的地图,缩放级别等,
如下图所示。

ios - 在UIView中正确显示Google map-LMLPHP

如何使UIView地图正确描绘?
ps。我用相同的结果尝试了viewDidLoad和viewDidAppear。
谢谢

最佳答案

您已经在为IBOutlet创建GMSMapView

它将为您创建一个地图实例。因此,无需再创建一个GMSMapView实例并将其分配给class变量。

override func viewDidAppear(_ animated: Bool) {
     // 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)

     self.myMapView.mapType =  .terrain

     self.myMapView.camera = camera


    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: +31.75097946, longitude: +35.23694368)
    marker.title = "Eden VidanPeled"

    //marker.snippet = "Australia"
    marker.map = self.myMapView
    marker.opacity = 1.0
}

关于ios - 在UIView中正确显示Google map ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44845048/

10-11 22:40
查看更多