Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps Mapview not centering on marker

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

like image 836
Shijo Avatar asked Jun 29 '18 17:06

Shijo


Video Answer


1 Answers

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.

like image 83
Awais Fayyaz Avatar answered Oct 01 '22 23:10

Awais Fayyaz