简体   繁体   English

通过MKMapView上的用户交互创建叠加层?

[英]Create overlay from user interaction on MKMapView?

I have two questions, 我有两个问题,

  1. How to create an overlay on a MKMapkitView from user's touch down events? 如何通过用户的触地事件在MKMapkitView上创建覆盖图? ie To keep it simple, the user touches down and it creates a MKCircle overlay 即为简单起见,用户按下并创建一个MKCircle叠加层

  2. How does the Maps application implements the "dropped pin" on touch down? Maps应用程序如何实现着陆时的“掉线”操作? Anybody knows or have some code examples on how to accomplish something similar? 有人知道或有一些关于如何完成类似任务的代码示例吗?

Any pointers would be greatly appreciated. 任何指针将不胜感激。 I've been googling and reading lots of docs without much success as you can see. 如您所见,我一直在使用Google搜索和阅读大量文档,但都没有取得很大的成功。

Below is an example that creates a circle and drops a pin where the user touches and holds their finger for 1 second. 下面是一个创建圆圈并在用户触摸并按住手指1秒钟的地方放下销钉的示例。 It uses a UILongPressGestureRecognizer which is added to the mapView wherever the map is initialized (eg. viewDidLoad). 它使用一个UILongPressGestureRecognizer,无论地图被初始化到何处(例如,viewDidLoad),它都会添加到mapView中。

Make sure the mapView's delegate is set also. 确保同时设置了mapView的委托。

// 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM