MKAnnotationView针不会出现 [英] MKAnnotationView pin wont appear

查看:57
本文介绍了MKAnnotationView针不会出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MapKit及其所有功能的新手,因此,我一直坚持尝试显示图钉.我遵循了有关如何查找用户当前位置并在此处放置图钉的在线视频教程.但是,当我输入图钉的方法时,我根本看不到它.我想知道我在哪里出错了.

I am new with MapKit and all its functionalities and therefore, stuck at trying to display a pin. I followed an online video tutorial on how to find user current location and drop a pin there. But when I typed the method for the pin, I do not get it at all. I would like to know where did I go wrong for this.

    - (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];

}

任何关于我做错地方的建议都是很棒的!

Any advice of where I done wrong would be great!

推荐答案

由于永远不会调用您的方法,因此应该执行以下操作:

As your method is never got invoked, you should do:

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

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

And you should create your MKAnnotationView properly. You need create two properties in .h file first (for performance reason):

MKPointAnnotation *mkShape;
MKAnnotationView *annotationView;

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

then use the code below to create your 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;
}

最后,不要忘记释放您的属性.

At the end, don't forget to release your properties.

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

    [super dealloc];
}

这篇关于MKAnnotationView针不会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆