【问题标题】:Add annotation pin on touch using MapKit and UIGestureRecognizer使用 MapKit 和 UIGestureRecognizer 在触摸时添加注释引脚
【发布时间】:2012-05-02 08:47:36
【问题描述】:

当用户触摸地图时,我在添加注释时遇到了一些问题。

我正在使用UIGestureRecognizer 来检测用户的触摸。

当检测到长按时,我正在调用这个函数:

- (void)handleLongPressGesture:(UIGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) return;

NSLog(@"long press");

CGPoint touchPoint = [gestureRecognizer locationInView:mapView];   
CLLocationCoordinate2D touchMapCoordinate = 
[mapView convertPoint:touchPoint toCoordinateFromView:mapView];

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init];

[rdvAnnotation initWithCoordinate:touchMapCoordinate];

[mapView removeAnnotations:mapView.annotations]; 

[mapView addAnnotation:rdvAnnotation];

[rdvAnnotation release]; }

我可以在控制台中看到 NSLog 并且 rdvAnnotation 被初始化为良好的坐标。

我不明白为什么我在地图上看不到我的注释。

这是我的- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation 方法:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation{
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

if ([annotation isKindOfClass:[RdvAnnotation class]]) 
{
    static NSString* RdvAnnotationIdentifier = @"rdvAnnotationIdentifier";
    MKPinAnnotationView* pinView = (MKPinAnnotationView *)
    [mapView dequeueReusableAnnotationViewWithIdentifier:RdvAnnotationIdentifier];

    if (!pinView)
    {
        MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                               initWithAnnotation:annotation reuseIdentifier:RdvAnnotationIdentifier] autorelease];
        customPinView.pinColor = MKPinAnnotationColorPurple;
        customPinView.animatesDrop = YES;
        customPinView.canShowCallout = YES;

        return customPinView;

    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}
return nil;}

我的viewDidLoad方法:

- (void)viewDidLoad {
[super viewDidLoad];

mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
[mapView setShowsUserLocation:TRUE];
[mapView setMapType:MKMapTypeStandard];
[mapView setDelegate:self];

CLLocationManager *locationManager=[[CLLocationManager alloc] init];

[locationManager setDelegate:self];

[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

[locationManager startUpdatingLocation];

self.navigationItem.title = @"Rendez-vous" ;    
}   

【问题讨论】:

  • 您实现了-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation MapView 委托吗?这是创建实际视图的地方。
  • 是的,我已经更新了我的问题。你看出什么不对了吗?
  • 似乎没问题。可能与手势位置到坐标转换有关吗?你能检查一下坐标是否在地图视图的可见区域内吗?
  • 查了一下,是的。我也尝试过最大程度地缩小,在任何地方都看不到注释。我无法弄清楚问题是什么。还有其他地方(在代码中)我应该检查吗?
  • mapView是如何声明和创建的?展示如何将手势识别器添加到地图视图中。在 addAnnotation 行之后,输入NSLog(@"ann count = %d", mapView.annotations.count); 并查看它的内容。您是否在代码中的其他任何地方调用 removeAnnotation(s)?

标签: objective-c ios mapkit uigesturerecognizer mkannotation


【解决方案1】:

我刚刚注意到一些奇怪的东西:

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init];
[rdvAnnotation initWithCoordinate:touchMapCoordinate];

您在注解对象上调用了两次 init。你应该这样做:

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] initWithCoordinate:touchMapCoordinate]];

编辑: 如果您的 init 方法中有不想丢失的代码,请保留 init 并更改坐标属性的值:

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init];
rdvAnnotation.coordinate = touchMapCoordinate;

【讨论】:

  • 我修复了这个错误,但它并没有解决我的问题。仍然没有我的新注释。事实上,第一个 init 初始化了我的对象,第二个将正确的坐标放入我的对象中。
  • 如果您在重载的 init 中有重要代码,请尝试我在答案中的编辑建议。
  • 也试过了,但还是没有。不过,我非常感谢您的帮助。
【解决方案2】:

viewDidLoad 中,您正在创建一个新的地图视图对象。

首先,这个新的地图视图对象没有作为子视图添加到self.view,因此它存在于内存中但不可见。

其次,您可能已经在 Interface Builder 的视图中放置了一个地图视图对象,因此您无需创建新对象。

所以可能发生的情况是,当您添加注释时,它被添加到内存中的地图视图对象,而不是可见的对象(在 IB 中创建的对象)。

不要在viewDidLoad 中创建新的地图视图,而是将mapView IBOutlet 连接到Interface Builder 中的地图视图。

【讨论】:

  • 好的,所以我已经在 ViewDidLoad 中删除了 mapView 上的声明。 mapView 在 IB 中连接良好,但还是看不到新的注解。
  • 在 .h 中,您是否将 mapView 定义为 @property (retain) IBOutlet MKMapView *mapView; 并且在 IB 中,文件所有者中的 mapView 插座是否连接到地图视图?手势识别器中的 NSLog 是否仍在显示?计数是否显示?
  • 在.h mapView 中定义为@property (retain) IBOutlet MKMapView *mapView;在 IB 中,mapView 出口连接到地图视图。在地图上按下时计数会增加(我已经删除了 removeAnnotation )。
  • xib 中只有一个地图视图,对吧?如果你暂时注释掉 viewForAnnotation 委托方法会发生什么(地图视图应该默认放置红色大头针)?如果您在 viewDidLoad 中(而不是在手势识别器中)手动添加带有某些特定坐标的注释怎么办?
  • 是的,只有一个。如果我注释掉 viewForAnnotation 委托方法,它不会改变任何东西。当我手动添加注释时,它不显示,但注释计数很好(按下后显示为 3:我的位置,手动添加的注释和按下后的注释)
猜你喜欢
  • 1970-01-01
  • 2012-12-20
  • 1970-01-01
  • 2013-04-06
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多