Idan Moshe
Idan Moshe

Reputation: 1533

Trigger when 'myLocation' button tapped (Google Maps SDK for iOS)

I want to do some stuff when 'myLocation' button tapped. So far, I've the UIButton itself:

UIButton *btnMyLoc = (UIButton*)[self.googleMapView subviews].lastObject;

But it's not enough. Any ideas?

Upvotes: 6

Views: 7352

Answers (5)

Davender Verma
Davender Verma

Reputation: 573

  func didTapMyLocationButton(for mapView: GMSMapView) -> Bool {


//if want to change the camera position

            if self.latDouble != 0.0 && self.longDouble != 0.0
            {
                let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: self.latDouble, longitude: self.longDouble, zoom: 18.0)
                self.mapView.camera = camera

            }

             self.mapView.isMyLocationEnabled = true
            self.mapView.reloadInputViews()
            return true
        }

Upvotes: 0

Devendra Singh
Devendra Singh

Reputation: 777

Delegate method available

func didTapMyLocationButton(for mapView: GMSMapView) -> Bool{

}

Upvotes: 2

DarkLeafyGreen
DarkLeafyGreen

Reputation: 70466

In release 1.9.2 there is a delegate method that does exactly that:

(BOOL) didTapMyLocationButtonForMapView: (GMSMapView *) mapView [optional]

Link to documentation

Upvotes: 19

Linh Nguyen
Linh Nguyen

Reputation: 2036

For do any thing other you also can get reference to Location Button.

// custom target My Location Button
    for (UIView* tmpview in _mapView.subviews[1].subviews[0].subviews) {
        if ([NSStringFromClass([[tmpview class] class]) isEqualToString:@"GMSx_QTMButton"]) {
            if ([tmpview isKindOfClass:[UIButton class]]) {
                myLocationBtn = (UIButton*)tmpview;
                [myLocationBtn addTarget:self action:@selector(clickedOnLocationButton:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
    }

Upvotes: 0

tony m
tony m

Reputation: 4779

Currently there is no direct method in Google Maps IOS sdk for knowing when the user taps on MyLocation button. A possible workaround is to use the below method

- (void) mapView: (GMSMapView *) mapView  idleAtCameraPosition: (GMSCameraPosition *)   position 

This will be called at the end of any camera animation or gestures. When a user clicks mylocation button, the camera will be animated to position the visible map region such that the user's current location(if detected) will lie at the center. So you can check inside idleAtCameraPosition whether the location is same as the user's current location that can be known through - (CLLocation*) myLocation [read, assign] and do your required functionality.

Upvotes: 1

Related Questions