【问题标题】:MKAnnotationView pin wont appearMKAnnotationView 引脚不会出现
【发布时间】:2011-08-11 07:37:39
【问题描述】:

我是 MapKit 及其所有功能的新手,因此一直在尝试显示图钉。我遵循了一个在线视频教程,介绍了如何找到用户当前位置并在那里放置一个图钉。但是当我输入引脚的方法时,我根本不明白。我想知道我哪里做错了。

    - (void)viewDidLoad{
    [super viewDidLoad];

    self.title = @"Location";
    mapView.showsUserLocation = YES;
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    [locationManager startUpdatingHeading];
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    NSLog(@"Annotation view run");
    MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    pin.animatesDrop = YES;
    return pin;
}

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

// once location is determined, center to that location.
location = newLocation.coordinate;
MKCoordinateRegion region;
region.center = location;

MKCoordinateSpan span;
span.latitudeDelta = 0.003;
span.longitudeDelta = 0.003;
region.span = span;

[mapView setRegion:region animated:FALSE];

}

任何关于我哪里做错的建议都会很棒!

【问题讨论】:

  • 您的日志中是否曾经显示过“注释视图运行”?
  • 不,甚至没有调用该方法。
  • 你在.h文件中设置了MKMapViewDelegate吗?
  • 是的,我也有,包括 CLLocationManagerDelegate。

标签: iphone mapkit


【解决方案1】:

由于您的方法永远不会被调用,您应该这样做:

- (void)mapView:(MKMapView *)aMapView regionDidChangeAnimated:(BOOL)animated {
    [aMapView removeAnnotation:[self point]];
    mkShape.coordinate = aMapView.centerCoordinate;
    [aMapView addAnnotation:mkShape];
}

你应该正确地创建你的 MKAnnotationView。 您需要首先在 .h 文件中创建两个属性(出于性能原因):

MKPointAnnotation *mkShape;
MKAnnotationView *annotationView;

然后使用下面的代码创建您的 MKPointAnnotation

- (void)createShape
{
    if (!mkShape) {
        mkShape = [[MKPointAnnotation alloc] init];
        mkShape.title = nil;
        mkShape.subtitle = @"test description";
    }
}

- (id <MKAnnotation>)point
{
    [self createShape];

    // Make sure to check if this is an MKPointAnnotation.  MKOverlays also
    // conform to MKAnnotation, so it isn't sufficient to just check to
    // conformance to MKAnnotation.
    if ([mkShape isKindOfClass:[MKPointAnnotation class]])
        return (id <MKAnnotation>)mkShape;

    return nil;
}

- (MKAnnotationView *)annotationView
{
    if (!annotationView) {
        id <MKAnnotation> annotation = [self point];
        if (annotation) {
            MKPinAnnotationView *pin =
            [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
            pin.canShowCallout = YES;
            pin.animatesDrop = YES;
            pin.draggable = NO;
            annotationView = pin;
        }
    }
    return annotationView;
}


- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isEqual:mkShape]) {
        return [self annotationView];
    }
    return nil;
}

最后,别忘了释放你的属性。

- (void)dealloc
{
    [annotationView release];
    [mkShape release];

    [super dealloc];
}

【讨论】:

  • 剂量 mapView:viewForAnnotation: 被调用?如果您使用模拟器测试您的代码,请尝试自己移动地图,并等待查看该方法是否被调用。
  • 顺便说一句,你设置过你的 mapView.delegate 吗?我找不到您在代码中的设置位置。
  • 第一条评论:不,它不会被调用。而且我不知道移动地图可能会调用它,现在再试第二条评论:是的,我希望我在 .h 文件中正确地做到了。我在界面中声明了MKMApViewDelegate,对吗?还是我必须做一个 mapView.delegate = self; ?
  • 是的,你必须添加 mapView.delegate = self;让 mapView 了解委托人在哪里。
  • 那么在.h中声明MKMapViewDelegate是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
  • 2013-08-03
  • 1970-01-01
  • 2021-12-03
相关资源
最近更新 更多