使用图像添加MKPointAnnotation时不会删除蓝点

时间:2014-01-02 06:44:13

标签: ios mkmapview

我正在使用MKMapView添加MKPointAnnotation注释。

我已将图像发送给MKPointAnnoation,但未删除蓝点。

-(void)updateMarkerWithUserLocation
{
    NSString *name=  [[NSUserDefaults standardUserDefaults]valueForKey:@"userName"];

    for (MKPointAnnotation *annotation in _mapView.annotations)
    {
        [_mapView removeAnnotation:annotation];
    }
    NSNumber * latitude = [NSNumber numberWithFloat:[[NSUserDefaults standardUserDefaults]floatForKey:@"currentLatitude"]];
    NSNumber * longitude = [NSNumber numberWithFloat:[[NSUserDefaults standardUserDefaults]floatForKey:@"currentLongitude"]];

    ////******   to add my location in the map of type MKPointAnnotation

    CLLocationCoordinate2D annotationCoord;

    annotationCoord.latitude = [latitude floatValue];
    annotationCoord.longitude = [longitude floatValue];

    MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
    annotationPoint.coordinate = annotationCoord;

    StrLocality=[[NSUserDefaults standardUserDefaults]valueForKey:@"StrLocality"];
    annotationPoint.title = name;
    annotationPoint.subtitle=StrLocality;

    [_mapView addAnnotation:annotationPoint];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        // Try to dequeue an existing pin view first.
        MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
        if (!pinView)
        {
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            //pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;
            pinView.image = [UIImage imageNamed:@"user_location.png"];
            // pinView.calloutOffset = CGPointMake(5, 32);
        } else {
            pinView.annotation = annotation;
        }
        return pinView;
    }

    return nil;
}

enter image description here

2 个答案:

答案 0 :(得分:2)

由于您已告知地图显示用户的位置,因此不会删除蓝点,直到您告诉它停止显示用户的位置,它将继续显示蓝点。 如果您想要这样做,请在代码中的某处添加[mapView setShowsuserLocation:NO]以将其关闭。

答案 1 :(得分:1)

要设置用户位置的自定义图像,请在viewForAnnotation方法中添加这些行,

if ([annotation isMemberOfClass:[MKUserLocation class]]) {
   //code to set current location pin's image
}