MKMapView not updating user position image

隐身守侯 提交于 2019-12-06 05:54:02

问题


My MKMapView shows my position at startup but then the image never 'follows' me. The location gets updated and the screen does follow me, but the original "User Location" image stays behind.

Here is some code snippets:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString* AnnotationIdentifier = @"Annotation";
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if(!pinView)
    {
        MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];

        if(annotation == mapView.userLocation) customPinView.image = [self rotate:[UIImage imageNamed:@"myCar.png"] orientation:UIImageOrientationUp];
        else customPinView.image = [UIImage imageNamed:@"randomPin.png"];

        customPinView.animatesDrop = NO;
        customPinView.canShowCallout = YES;
        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}

-(void)locationUpdate:(CLLocation *)location
{
    CLLocationCoordinate2D loc = [location coordinate];
    if(isFollowing)
        [myMapView setCenterCoordinate:loc];//Works
}

and in my viewDidLoad I do call: [myMapView setShowsUserLocation:YES]; which does work.

So basically somewhere I neglect updating my position or its most possibly where I draw the new image for my current position.

Can anyone possibly see what I am missing or doing wrong there for it to not follow my location updates?

Thanks.


回答1:


It's not clear if this is the issue but the viewForAnnotation method doesn't look right.

The annotation image is only being set when an annotation view is created. If a view is re-used, the annotation property is updated but not the image. It's possible that the re-used view is for an annotation of a different type requiring a different image.

The method should look something like this:

-(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.animatesDrop = NO;
        pinView.canShowCallout = YES;
    }
    else
    {
        pinView.annotation = annotation;
    }

    if (annotation == mapView.userLocation) 
        pinView.image = [self rotate:[UIImage imageNamed:@"myCar.png"] orientation:UIImageOrientationUp];
    else 
        pinView.image = [UIImage imageNamed:@"randomPin.png"];

    return pinView;
}


来源:https://stackoverflow.com/questions/6404884/mkmapview-not-updating-user-position-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!