Google地图SDK在iOS 7中未能调用didTapMyLocationButtonForMapView函数

3
我正在一个使用iOS 7的项目中使用Google Maps SDK 1.7.2(我刚从iOS6升级)。由于某些原因,所有的GMSMapViewDelegate回调都有效,但是this则不行。
- (BOOL) didTapMyLocationButtonForMapView: (GMSMapView *)mapView

enter image description here

我假设这应该在箭头按钮被点击时调用,对吗?有任何想法为什么没有呢?
这是我实例化地图视图的方式:
mapView_ = [GMSMapView mapWithFrame:[[self mainView] bounds] 
                             camera:[self currentCameraUseStandartZoom:YES]];
[[mapView_ settings] setMyLocationButton:YES];
[mapView_ setDelegate:self];
[[self mainView] addSubview:mapView_];
2个回答

2

我使用了一种相当可靠的方法来查找“我的位置”按钮。

最初在创建地图视图时,此按钮被隐藏。然后我通过地图视图层次结构查找所有隐藏的按钮。接着我设置self.mapView.settings.myLocationButton = YES并检查我找到的所有按钮中是否有任何一个不再隐藏。

这是我使用的代码:

- (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];
        }
    }
}

最后,
- (void)viewDidLoad
{
    ...
    UIButton* myLocationButton = [self findAndShowMyLocationButton];
    [myLocationButton addTarget:self action:@selector(myLocationClick) forControlEvents:UIControlEventTouchUpInside];
}

但问题是为什么方法没有被调用!!而不是按钮为什么隐藏! - Aviram Netanel
@AviramNetanel,这就是为什么我的答案不是关于按钮为什么隐藏的原因。 - deej
我的问题最终是由于iOS8的更改引起的。 - Aviram Netanel

0

以防有人遇到相同的问题...我用了一个hack方法基本上解决了这个问题...以下是代码:

在我的main.m文件中,我自定义了UIResponder类:

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

以防你不知道,TPMUMapStateInMotionAsResultOfMyLocationButtonTap是一个状态机变量的状态,有以下状态:

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

因为我想要通过位置按钮的点击或用户手势来跟踪地图中的运动。

希望这可以帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接