【发布时间】: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 <MKAnnotation>)annotationMapView 委托吗?这是创建实际视图的地方。 -
是的,我已经更新了我的问题。你看出什么不对了吗?
-
似乎没问题。可能与手势位置到坐标转换有关吗?你能检查一下坐标是否在地图视图的可见区域内吗?
-
查了一下,是的。我也尝试过最大程度地缩小,在任何地方都看不到注释。我无法弄清楚问题是什么。还有其他地方(在代码中)我应该检查吗?
-
mapView是如何声明和创建的?展示如何将手势识别器添加到地图视图中。在 addAnnotation 行之后,输入
NSLog(@"ann count = %d", mapView.annotations.count);并查看它的内容。您是否在代码中的其他任何地方调用 removeAnnotation(s)?
标签: objective-c ios mapkit uigesturerecognizer mkannotation