Pooja Shah
Pooja Shah

Reputation: 997

How to detect camera position for google map in swift

I get to know after searching that idleAtCameraPosition and didChangeCameraPosition this methods are called when the map moves . How can i write this method for swift? I have set delegate to CLLocationManagerDelegate and GMSMapViewDelegate but still this methods are not called. I have written method as below:

func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!) {
                print(position)
}

func mapView(mapView:GMSMapView!,idleAtCameraPosition position:GMSCameraPosition!)
{
    print(position)

}

But my method is not getting called. Also I want to customise info window just like done here : Custom Info Window for Google Maps . But can't find the methods how to do same in swift. I am new to swift s o not getting this.

Upvotes: 3

Views: 6727

Answers (2)

Tommy
Tommy

Reputation: 989

You need to import the GMSMapViewDelegate at the top of your class

put self.mapView.delegate = self in your viewDidLoad()

and then you can use this method

func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) {

        // You can use the position.target for get the current camera position

}

Upvotes: 4

Hernan Duque
Hernan Duque

Reputation: 73

Maybe too late, but you must extend the class with GMSMapViewDelegate. For example:

class NearMeViewController: CLLocationManagerDelegate, GMSMapViewDelegate 

Then, how Ztan says, you have to delegate your map with:

self.mapView.delegate = self

With this, the method didChangeCameraPosition will respond every time that that camera's position is changed.

Hope it helps.

Upvotes: 7

Related Questions