iOS:用户位置变为pin

时间:2013-07-03 08:56:21

标签: ios mkmapview

我有一个带有奇怪行为的mapView:当我打开它时,一切正常。将找到用户(蓝色圆圈)并且3个针脚就位。但是(我不知道为什么)经过一段时间后,蓝点变成了一个引脚 - 但只有当我的连接速度很慢时。

这是我得到的:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];

    if (!pinView && ![annotation isKindOfClass:[MKUserLocation class]])
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.animatesDrop = YES;
        pinView.canShowCallout = YES;

        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinView.rightCalloutAccessoryView = rightButton;

        if (annotation == self.locationZH1)
        {
            [pinView setTag:1];
        }
        else if (annotation == self.locationZH2)
        {
            [pinView setTag:2];
        }
        else if (annotation == self.locationZH3)
        {
            [pinView setTag:3];
        }
        else if (annotation == self.locationLU1)
        {
            [pinView setTag:4];
        }
    }
    else
    {
        pinView.annotation = annotation;
    }

    return pinView;
}

3 个答案:

答案 0 :(得分:9)

请务必避免为内置用户位置标记提供注释视图。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    //check annotation is not user location
    if([annotation isEqual:[mapView userLocation]])
    {
        //bail
        return nil;
    }

    static NSString *annotationViewReuseIdentifer = @"map_view_annotation";

    //dequeue annotation view
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifer];

    if(!annotationView)
    {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifer];
    }

    //set annotation view properties
    [annotationView setAnnotation:annotation];

    return annotationView;
}

通过先检查用户位置注释,您可以提前返回nil,而不是分配新的MKPinAnnotationView并返回。{/ p>

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation州的文档:

  

如果注释参数中的对象是。的实例   MKUserLocation类,可以提供自定义视图来表示   用户的位置。使用默认值显示用户的位置   系统视图,返回nil。

答案 1 :(得分:4)

Swift 3解决方案:

if annotation is MKUserLocation{
    return nil
}

答案 2 :(得分:0)

您需要在注释== mapView.userLocation时返回nil,以显示用户位置的蓝点及其周围的圆圈

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

    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView  dequeueReusableAnnotationViewWithIdentifier:@"pinView"];

    if (!pinView && ![annotation isKindOfClass:[MKUserLocation class]])
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.animatesDrop = YES;
        pinView.canShowCallout = YES;

        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinView.rightCalloutAccessoryView = rightButton;

        if (annotation == self.locationZH1)
        {
            [pinView setTag:1];
        }
        else if (annotation == self.locationZH2)
        {
            [pinView setTag:2];
        }
        else if (annotation == self.locationZH3)
        {
            [pinView setTag:3];
        }
        else if (annotation == self.locationLU1)
        {
            [pinView setTag:4];
        }
        return pinView;
    }
    else
    {
        pinView.annotation = annotation;
        return Nil;
    }
}