Get Latitude and longitude center of Google Map Get Latitude and longitude center of Google Map swift swift

Get Latitude and longitude center of Google Map


You are doing it right to get the center of the map by using the google maps's func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) delegate.

Take a variable for center coordinates

var centerMapCoordinate:CLLocationCoordinate2D!

Implement this delegate to know the center position.

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {    let latitude = mapView.camera.target.latitude    let longitude = mapView.camera.target.longitude    centerMapCoordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)    self.placeMarkerOnCenter(centerMapCoordinate:centerMapCoordinate)}

Function to place a marker on center point

func placeMarkerOnCenter(centerMapCoordinate:CLLocationCoordinate2D) {    let marker = GMSMarker()    marker.position = centerMapCoordinate    marker.map = self.mapView}

In this case you will get a lot of markers. So keep a global hold of marker and check if it is already present, just change the position

var marker:GMSMarker!func placeMarkerOnCenter(centerMapCoordinate:CLLocationCoordinate2D) {    if marker == nil {        marker = GMSMarker()    }    marker.position = centerMapCoordinate    marker.map = self.mapView}