I am using google maps SDK for ios. I want to make a marker fixed in the center of the screen, so when user drags the map, the marker does not move and stays in the center. I am also trying to read the coordinate of the center after the drag. Any input would be appreciated. Thanks!
I would rather display view on top of GMSMapView (do not use markers for that). Since you have screen position for map view, that should be easy to place your view in correct position.
To get coordinates you can use mapView.projection.coordinateForPoint
Documentation is here
To know that drag is finished, make you view controller (or any other object) delegate of map view (GMSMapViewDelegate
) and implement mapView:idleAtCameraPosition:
method.
Docs are here
Create outlet of GMSMapView and attached Image at the center of the map and search bar at the top to present the location name.
Drag the map on Device, which will trigger 2 delegate method of GMSMapViewDelegate.
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition)
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition)
/** * Called repeatedly during any animations or gestures on the map (or once, if the camera is * explicitly set). This may not be called for all intermediate camera positions. It is always * called for the final position of an animation or gesture. */ func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) { print("didchange")> > returnPostionOfMapView(mapView: mapView) } /** * Called when the map becomes idle, after any outstanding gestures or animations have completed (or * after the camera has been explicitly set). */ func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) { print("idleAt") //called when the map is idle returnPostionOfMapView(mapView: mapView) } //Convert the location position to address func returnPostionOfMapView(mapView:GMSMapView){ let geocoder = GMSGeocoder() let latitute = mapView.camera.target.latitude let longitude = mapView.camera.target.longitude let position = CLLocationCoordinate2DMake(latitute, longitude) geocoder.reverseGeocodeCoordinate(position) { response , error in if error != nil { print("GMSReverseGeocode Error: \(String(describing: error?.localizedDescription))") }else { let result = response?.results()?.first let address = result?.lines?.reduce("") { $0 == "" ? $1 : $0 + ", " + $1 } self.searchBar.text = address } } }
Github Demo Project
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With