2011-03-07 27 views
1

我有兩個問題,在MKMapView上創建來自用戶交互的覆蓋圖?

  1. 如何創建用戶的觸摸按下事件上MKMapkitView覆蓋?即爲了保持它的簡單,用戶觸摸下來,它創建了一個MKCircle覆蓋

  2. 如何進行地圖應用程序實現對觸摸的「圖釘」下來?任何人都知道或有一些關於如何完成類似事情的代碼示例?

任何指針將不勝感激。如你所見,我一直在使用Google和閱讀大量文檔。

回答

11

下面是創建一個circle和下降的銷,其中用戶觸摸並保持他們的手指1秒的例子。它使用一個UILongPressGestureRecognizer,它在地圖初始化的任何地方被添加到mapView中(例如viewDidLoad)。

確認的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; 
} 
+0

大!我只是在尋找指針,但代碼示例更好。真的很感激它。 – jlmendezbonini 2011-03-07 22:37:40