iphone - 从 MKMapView 上的用户交互创建覆盖?

标签 iphone ios ios4 mkmapview

我有两个问题,

  • 如何根据用户的触摸事件在 MKMapkitView 上创建叠加层?即,为了简单起见,用户按下并创建一个 MKCircle 覆盖
  • Maps 应用程序如何在触地时实现“dropped pin”?任何人都知道或有一些关于如何完成类似事情的代码示例?

  • 任何指针将不胜感激。如您所见,我一直在谷歌搜索和阅读大量文档,但没有取得多大成功。

    最佳答案

    下面是一个示例,它创建一个圆圈并在用户触摸并按住手指 1 秒钟的位置放置一个图钉。它使用了一个 UILongPressGestureRecognizer,它被添加到 mapView 的任何初始化 map 的位置(例如 viewDidLoad)。

    确保也设置了 mapView 的委托(delegate)。

    // In viewDidLoad or where map is initialized...
    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 1.0;  //user must hold for 1 second
    [mapView addGestureRecognizer:lpgr];
    [lpgr release];
    
    ...
    
    - (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
    {
        if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
            return;
    
        CGPoint touchPoint = [gestureRecognizer locationInView:mapView];    
        CLLocationCoordinate2D touchMapCoordinate = [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
    
        //add pin where user touched down...
        MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
        pa.coordinate = touchMapCoordinate;
        pa.title = @"Hello";
        [mapView addAnnotation:pa];
        [pa release];
    
        //add circle with 5km radius where user touched down...
        MKCircle *circle = [MKCircle circleWithCenterCoordinate:touchMapCoordinate radius:5000];
        [mapView addOverlay:circle];
    }
    
    -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay 
    {
        MKCircleView* circleView = [[[MKCircleView alloc] initWithOverlay:overlay] autorelease];
        circleView.fillColor = [UIColor redColor];
        return circleView;
    }
    
    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        static NSString *AnnotationIdentifier = @"Annotation";
        MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
        if (!pinView)
        {
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
            pinView.pinColor = MKPinAnnotationColorGreen;
            pinView.animatesDrop = YES;
        }
        else
        {
            pinView.annotation = annotation;
        }
        return pinView;
    }
    

    关于iphone - 从 MKMapView 上的用户交互创建覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5223195/

    相关文章:

    ios - 使用数字格式化程序指定文本字段中的小数位数

    iOS 创建 PDF 时添加链接

    iphone - 编辑 TableView 时自定义 TableCell 中没有缩进

    objective-c - 方向更改后居中 UIPageControl

    ios - AVSpeechSynthesizer 输出为文件?

    iphone - 根据 iOS 版本禁用委托(delegate)方法

    iphone - iOS UIView block 动画停止实际动画

    php - 如何知道 .pem 文件是否正确

    ios - 如何在 iOS 上显示 pdf

    iphone - 相机覆盖标签问题