用户位置显示为注释

时间:2014-04-24 10:19:15

标签: ios annotations userlocation

我在MapView中有多个注释,列在3个不同的数组中。 我已使用- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation方法更改注释的标注。

奇怪的是我的UserLocation正在变成一个Customized Annotation。 为什么会这样,可能是什么问题?

我如何列出我的注释:

myAnn = [[Annotations alloc]init];
location.latitude = 52.338847;
location.longitude = 4.937482;
myAnn.coordinate = location;
myAnn.title = @"Afvalpunt";
myAnn.subtitle = @"Rozenburglaan 1";
[category3 addObject:myAnn];

[self.locationArrays addObject:category3];

self.currentAnnotation = 0;

[self.myMapView addAnnotations:[self.locationArrays objectAtIndex:0]];

我的方法是如何设置的:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapAn"];
    if (!annotationView) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapAn"];
        annotationView.canShowCallout = YES;

        //Blauw Navigatie Auto...
        UIImageView *carView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Driving"]];
        UIButton *blueView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44+30)];
        blueView.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:1];
        carView.frame = CGRectMake(11, 14, carView.image.size.width, carView.image.size.height);
        [blueView addTarget:self action:@selector(carClicked) forControlEvents:UIControlEventTouchUpInside];
        [blueView addSubview:carView];
        annotationView.leftCalloutAccessoryView = blueView;
    } 
    return annotationView;
}

2 个答案:

答案 0 :(得分:1)

试试这段代码。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    if (annotation == mapView.userLocation) return nil;
    ...

如果注释 userLocation ,我们返回 nil ,让 mapView 显示蓝点&amp;圆圈动画。要显示 userLocation 的自定义注释,只需删除行return nil;并在那里进行自定义。

- (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 = [UIImage imageNamed:@"myCarImage.png"];
    }
    else
    {
        customPinView.image = [UIImage imageNamed:@"mySomeOtherImage.png"];
        customPinView.animatesDrop = NO;
        customPinView.canShowCallout = YES;
        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }

    return pinView;
}

希望此代码对您有用。

答案 1 :(得分:1)

试试此代码

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{
     if ([annotation isKindOfClass:[MKUserLocation class]] || annotation==mapView.userLocation)
    {
        return nil;//Here you can also customise your pin if you dont want pin then just return nil.
    }
    else
    {
     //your code
    }
}