8

I am using google maps in ios with swift 4, i have to show a mapview in the screen, with a marker, the problem is that whenever i show the mapview i got this result:

enter image description here

and the result i want to get is this:

enter image description here

here is the code i am using:

 func loadMap(location:CLLocation){
    self.mapView.isMyLocationEnabled = true
    self.mapView.isUserInteractionEnabled = true
    self.view.layoutIfNeeded()
    self.addLocationMarker()
}

func addLocationMarker(){
    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    print(self.startLocation)
    marker.position = CLLocationCoordinate2D(latitude: self.startLocation.coordinate.latitude, longitude: self.startLocation.coordinate.longitude)
    marker.map = self.mapView

    let camera = GMSCameraPosition.camera(withLatitude: self.startLocation.coordinate.latitude, longitude: self.startLocation.coordinate.longitude, zoom: 12.0)
    self.mapView.camera = camera
}

and the mapview is an outlet of a view in the storyboard

Shijo
  • 115
  • 2
  • 7

1 Answers1

5

This Code works fine for me. Try this out (Swift 4)

func setupMapView() {

    DispatchQueue.main.async {
      let position = CLLocationCoordinate2D(latitude: 32.9483364, longitude: -96.8241543)
      let marker = GMSMarker(position: position)
      self.mapView.camera = GMSCameraPosition(target: position, zoom: 15, bearing: 0, viewingAngle: 0)
      marker.title = "Enter marker title here"
      marker.map = self.mapView
    }  
}

Note: I have used sample longitude and lattitude. You should insert your's.

Awais Fayyaz
  • 2,275
  • 1
  • 22
  • 45
  • 1
    This solutions works and `DispatchQueue.main.async` is the most important part of this solution. It looks like `GMSMapView` needs time to instantiate correct frame size. – Kamil Powałowski Dec 28 '18 at 10:17