gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:19:59 24 4
gpt4 key购买 nike

我有两个问题,

  • 如何根据用户的触摸事件在 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com