Ahmed
Ahmed

Reputation: 1359

How to add GMSMapView to a portion of a view in swift 4

I would like to add GMSMapView to a currentView as subview however it is showing a black page here is my code

    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)

If I change viewTest.addSubview(mapView) toself.view = mapView it works fine with the map showing on my wholeview.

If I change viewTest = mapView. I got a white screen without map showing with the following error.

" current[11927:300222] [BoringSSL] Function nw_protocol_boringssl_input_finished: line 1436 Peer disconnected during the middle of a handshake. Sending errSSLFatalAlert(-9802) alert 2018-06-23 00:56:12.499396-0400 current[11927:300222] TIC TCP Conn Failed [1:0x604000179c80]: 3:-9802 Err(-9802) "

Upvotes: 0

Views: 1075

Answers (1)

Mahendra
Mahendra

Reputation: 8914

You can create an IBOutlet of a view and set its class to GMSMapView.

enter image description here

Create the property for the same.

@IBOutlet weak var view_mapContainer: GMSMapView!

And finally you can set its properties.

  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

Upvotes: 3

Related Questions