如何在Swift 4中将GMSMapView添加到视图的一部分

时间:2018-06-23 04:56:54

标签: ios google-maps swift4 gmsmapview

我想将GMSMapView添加到currentView作为子视图,但是它显示的是黑色页面,这是我的代码

    let viewTest = UIView()
    viewTest.frame = CGRect.init(x: 16, y: 50, width: 300, height: 100)

    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

    mapView.delegate = self
    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView
    viewTest.addSubview(mapView)

如果我将viewTest.addSubview(mapView)更改为self.view = mapView,则可以在我的整体视图上显示地图。

如果我更改viewTest = mapView。我得到了白色屏幕,没有地图显示,并显示以下错误。

  

“ current [11927:300222] [BoringSSL]函数nw_protocol_boringssl_input_finished:行1436在握手过程中对等方断开连接。发送errSSLFatalAlert(-9802)警报   2018-06-23 00:56:12.499396-0400当前[11927:300222] TI​​C TCP连接失败[1:0x604000179c80]:3:-9802 Err(-9802)   “

1 个答案:

答案 0 :(得分:2)

您可以创建视图的IBOutlet并将其类设置为GMSMapView

enter image description here

为此创建属性。

@IBOutlet weak var view_mapContainer: GMSMapView!

最后,您可以设置其属性。

  let coord = CLLocationCoordinate2D(latitude: 12.123312, longitude: 76.123123) //set lat long of the location you want to set

  self.view_mapContainer.camera = GMSCameraPosition(target: coord, zoom: 13, bearing: 0, viewingAngle: 0)

  let marker = GMSMarker()
  marker.icon = UIImage(named: "ImageToSet")

  marker.appearAnimation = GMSMarkerAnimation(rawValue: 1)!
  marker.position = coord
  marker.title = "any title"
  marker.snippet = "any snippet"
  marker.map = self.view_mapContainer