Question

I'm using google maps SDK 1.7.2 on a projectd using iOS 7 (I just upgraded it from iOS6). For some reason all the GMSMapViewDelegate callbacks work but this one

- (BOOL) didTapMyLocationButtonForMapView: (GMSMapView *)mapView

enter image description here

I'm assuming this should be called when the arrow button is tapped right? Any idea why it isn't?

This is how I instantiate the mapsview:

mapView_ = [GMSMapView mapWithFrame:[[self mainView] bounds] 
                             camera:[self currentCameraUseStandartZoom:YES]];
[[mapView_ settings] setMyLocationButton:YES];
[mapView_ setDelegate:self];
[[self mainView] addSubview:mapView_];
Was it helpful?

Solution 2

just in case someone is having the same problem.. i pretty much resolved this by using a hack.. here it is:

in my main.m file I customized the UIResponder class:

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv,  NSStringFromClass([TPMUApplication class]), NSStringFromClass([TPMUAppDelegate class]));

    }
}

TPMUApplication.h

#import <UIKit/UIKit.h>
@class TPMUTaxiRequestVC;

@interface TPMUApplication : UIApplication
// basically the view controller that will be informed that 
// the user has tapped the my location button, normally it would subscribe to
// the GMSMapViewDelegate protocol, and it should have a GMSMapView property
@property (nonatomic, strong) TPMUTaxiRequestVC *taxiRequestVC;

@end

TPMUApplication.m

#import "TPMUApplication.h"
#import "TPMUTaxiRequestVC.h"

@implementation TPMUApplication
- (void)sendEvent:(UIEvent *)event
{
    [super sendEvent:event];
    UIView *touchReceipientView =((UITouch *)[event.allTouches anyObject]).view;
    NSLog(@"");
    CGRect myLocationButtonFourchIncFrame = CGRectMake(256, 525, 64, 54);
    CGRect myLocationButtonThreeHalfIncFrame = CGRectMake(256, 336, 64, 54);
    if (CGRectEqualToRect(touchReceipientView.frame, myLocationButtonFourchIncFrame) ||
        CGRectEqualToRect(touchReceipientView.frame, myLocationButtonThreeHalfIncFrame)) {
        if (self.taxiRequestVC.mapState != TPMUMapStateInMotionAsResultOfMyLocationButtonTap) {
            self.taxiRequestVC.mapState = TPMUMapStateInMotionAsResultOfMyLocationButtonTap;
            // notice that didTapMyLocationButtonForMapView is actually 
            // a method in the GMSMapViewDelegate protocol.. and since 
            // taxiRequestVC subscribes to that protocol.. we simply call it here
            // as if it was natively called
            [self.taxiRequestVC didTapMyLocationButtonForMapView:self.taxiRequestVC.mapView];
        }
    }

}
@end

and just in case you were wondering, TPMUMapStateInMotionAsResultOfMyLocationButtonTap is a state of a state machine variable with the following states:

typedef enum
{
    TPMUMapStateIdle = 0,
    TPMUMapStateInMotionAsResultOfUserGesture,
    TPMUMapStateInMotionAsResultOfMyLocationButtonTap
} TPMUMapState;

since I wanted to track motion in the map as a result of location button tap vs user gesture.

hope this helps!

OTHER TIPS

I use a pretty reliable method to find the my location button.

Initially when a map view is created the button is hidden. And I go through map view hierarchy to find all hidden buttons. Then I set self.mapView.settings.myLocationButton = YES and check if any of the buttons I found is not hidden anymore.

Here is the code I use:

- (UIButton*)findAndShowMyLocationButton
{
    NSMutableArray* hiddenButtons = [NSMutableArray array];
    [self findHiddenButtonsInView:self.mapView hiddenButtons:hiddenButtons];
    self.mapView.settings.myLocationButton = YES;
    for (UIButton* button in hiddenButtons) {
        if (!button.hidden) return button;
    }
    return nil;
}

- (void)findHiddenButtonsInView:(UIView*)view hiddenButtons:(NSMutableArray*)hiddenButtons
{
    for (UIView* subview in view.subviews) {
        if (subview.hidden && [subview isKindOfClass:[UIButton class]]) {
            [hiddenButtons addObject:subview];
        } else {
            [self findHiddenButtonsInView:subview hiddenButtons:hiddenButtons];
        }
    }
}

And finally

- (void)viewDidLoad
{
    ...
    UIButton* myLocationButton = [self findAndShowMyLocationButton];
    [myLocationButton addTarget:self action:@selector(myLocationClick) forControlEvents:UIControlEventTouchUpInside];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top