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

时间:2012-09-05 10:52:08

标签: iphone cocoa-touch mkmapview

我有 MapView ..我将其添加为ViewController的View的子视图。我在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 ..但它不起作用..我还需要做更多的事吗?

3 个答案:

答案 0 :(得分:1)

尝试在[self.view addSubview:mapView];

之后添加[mapView addGestureRecognizer:longPressGesture];
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];

答案 1 :(得分:1)

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

答案 2 :(得分:0)

试试这个:

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

[self.mapView addGestureRecognizer:lpgr];

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