在Mapview中添加带有按下位置坐标的Alertview



Im拥有MapView。。我将其添加为ViewController视图的子视图。我在ViewDidLoad中有以下代码:

[self.view addSubview:mapView];
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(mapLongPress:)]; 
    longPressGesture.minimumPressDuration = 2; 
    [mapView addGestureRecognizer:longPressGesture];
    [longPressGesture release];

而且,

- (void)mapLongPress:(UILongPressGestureRecognizer *)gestureRecognizer{
    NSLog(@"Gesture");
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
        CGPoint touchLocation = [gestureRecognizer locationInView:mapView];
        CLLocationCoordinate2D coordinate;
        coordinate = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];

这些是我从StackOverFlow得到的。。但它不起作用。。我还需要做些什么吗?

尝试在[mapView addGestureRecognizer:longPressGesture]; 之后添加[self.view addSubview:mapView];

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(mapLongPress:)]; 
longPressGesture.minimumPressDuration = 2; 
longPressGesture.delegate = self;
[self.mapView addGestureRecognizer:longPressGesture];
[self.view addSubview:mapView];
[longPressGesture release];

只需将UIGestureRecognizerDelegate添加到ViewController并执行我给出的上述代码!!!

试试这个:

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(mapLongPress:)];
lpgr.minimumPressDuration = 2.0;
[self.mapView addGestureRecognizer:lpgr];

这是在使用ARC,所以我不确定你现在是否应该释放这个手势。试着不松开它,然后试着松开它。看看这是否会影响手势。

最新更新