【问题标题】:ios: Dont have current location become a pin on mapios:不要让当前位置成为地图上的图钉
【发布时间】:2013-06-25 19:49:24
【问题描述】:

我在地图上放置了大头针,当前位置以蓝点显示。我补充说:

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


    NSString *identifier =@"mypin";
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [amapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if(pin ==nil){
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
    }else{
        pin.annotation = annotation;
    }

    UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    myDetailButton.frame = CGRectMake(0, 0, 23, 23);
    myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    [myDetailButton addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

    pin.rightCalloutAccessoryView = myDetailButton;
    pin.enabled = YES;
    pin.animatesDrop = TRUE;
    pin.canShowCallout = YES;

    return pin;
}

但现在当前位置是大头针而不是蓝点。如何阻止当前位置的注释成为图钉并将其保留为蓝点。

有什么帮助吗?

【问题讨论】:

  • 如何给地图添加注释?

标签: ios mkmapview apple-maps


【解决方案1】:

“蓝点”是一个特殊的注解,MKUserLocation。因此,在您的viewForAnnotation 中,只需在开头添加以下两行,告诉 iOS 使用标准的“蓝点”作为用户位置注释:

if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

通过返回nil,您将告诉iOS 为用户位置使用默认的“蓝点”注释。

有关实践中的示例,请参阅位置感知编程指南的Creating Annotation Views from Your Delegate Object 部分中的代码示例。

【讨论】:

    【解决方案2】:

    简单:

    - (MKAnnotationView *)mapView :(MKMapView *)maapView viewForAnnotation:(id <MKAnnotation>) annotation{
        @autoreleasepool {
    
    
        if (annotation == maapView.userLocation)
        {
            // This code will execute when the current location is called.
            return nil;
        }
        else
        {
            MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
            annView.pinColor = MKPinAnnotationColorPurple;
            annView.animatesDrop=YES;
            annView.canShowCallout = YES;
            annView.calloutOffset = CGPointMake(-5, 5);
            UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    
            annView.rightCalloutAccessoryView = rightButton;
            return annView;
        }
    
    }
    }
    

    【讨论】:

    • 为什么是自动释放池?此外,检查类成员而不是指针的相等性通常更安全。它恰好在这种情况下有效,但类成员身份更安全。
    【解决方案3】:

    干净且快速的 (3) 方式

    if annotation is MKUserLocation {
        return nil
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多