Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed marker on google maps for ios

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!

like image 944
Yini Avatar asked May 25 '15 15:05

Yini


2 Answers

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

like image 123
Piotr Avatar answered Oct 15 '22 16:10

Piotr


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
            }
        }
    }

enter image description here

Github Demo Project

like image 23
Shrawan Avatar answered Oct 15 '22 18:10

Shrawan