0

我正在开发一个 IOS 应用程序,它使用谷歌地图 SDK 在其中呈现地图。我实际上有一个视图控制器,它包含另一个实际上是处理地图渲染的视图控制器。我想要实现的是在用户移动地图相机并结束触摸它之后执行一些操作。我看到这种特殊情况的最佳选择是覆盖 touchesEnded:withEvent: 方法。我在包含的视图控制器中覆盖了这个方法,但由于某种原因它没有被触发。我的问题应该是什么原因?

btw mapView:idleAtCameraPosition: 不符合我的要求,因为我需要在用户释放触摸屏幕时执行操作(停止移动地图)

这是一些代码。它与google maps SDK for IOS提供的示例基本相同

界面

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <GoogleMaps/GoogleMaps.h>

@interface MTMapViewController : UIViewController <CLLocationManagerDelegate, GMSMapViewDelegate>

@property (nonatomic, strong) CLLocationManager *manager;

@end

执行

#import "MTMapViewController.h"
#import <GoogleMaps/Googlemaps.h>
#import <CoreLocation/CoreLocation.h>

@implementation MTMapViewController {
    GMSMapView *mapView_;
    GMSMarker *marker;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)loadView {
    [super loadView];
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:nil];
    mapView_.delegate = self;
    mapView_.myLocationEnabled = YES;
    mapView_.mapType = kGMSTypeNormal;
    mapView_.settings.myLocationButton = YES;
    mapView_.settings.compassButton = YES;
    self.view = mapView_;
    self.manager = [[CLLocationManager alloc] init];
    self.manager.delegate = self;
    [self.manager startUpdatingLocation];
    marker = [[GMSMarker alloc] init];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [manager stopUpdatingLocation];
    CLLocation *currentLocation = [self.manager location];
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:currentLocation.coordinate
                                                               zoom:17];
    mapView_.camera = camera;
    marker.position = currentLocation.coordinate;
    marker.icon = [UIImage imageNamed:@"passenger_marker.png"];
    marker.map = mapView_;
}

- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
    marker.position = position.target;
}

- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position {
    NSLog(@"mapView:idleAtCameraPosition fired");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    //This wont be invoked
    NSLog(@"touchesEnded:withEvent: fired");
}

@end

谢谢您的帮助

4

1 回答 1

1

当心 idleAtCameraPosition 是地图停止移动时而不是当您抬起手指时。

我必须继承 GSMapView 并添加 PanGestureRecognizer >> State ENDED 才能在手指抬起时得到。

https://github.com/clearbrian/GoogleMapiOS_TapEndedGesture

于 2014-02-21T12:45:45.457 回答