在 iPhone MKMapView 中显示用户位置蓝点

发布于 2024-11-08 14:52:52 字数 2450 浏览 4 评论 0原文

我正在 iPhone 应用程序的 MapView 中开发自定义引脚。 这是代码:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id  <MKAnnotation>)annotation
 {
     static NSString *AnnotationViewID = @"annotationViewID";

     MKAnnotationView *annotationView = (MKAnnotationView *)[myMapView  dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

     if (annotationView == nil)
     {
         annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
     }

    if ([[annotation title] isEqualToString:NSLocalizedString(@"Current Location",@"")])  {

        MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
        //pin.pinColor = MKPinAnnotationColorRed;
        annotationView = pin;
    }
    else
    {
        NSString *pin = [NSString stringWithFormat:@"pin%i.png",[[annotation imgNumber] intValue]];
        annotationView.image = [UIImage imageNamed:pin];//Canviar la chincheta per numero
        UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [annotationView setRightCalloutAccessoryView:button];
        annotationView.annotation = annotation;
        annotationView.canShowCallout = YES;

    /********* Imagen a la izquierda en la burbuja *********/
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    // Set up the Left callout
    UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myDetailButton.frame = CGRectMake(0, 0, 23, 23);
    myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    // Set the image for the button
    //[myDetailButton setImage:[UIImage imageNamed:@"botocorreu.png"] forState:UIControlStateNormal];
    NSString *detailButton = [NSString stringWithFormat:@"%i",[[annotation imgNumber] intValue]];
    [myDetailButton setImage:[UIImage imageNamed:detailButton] forState:UIControlStateNormal];  
    // Set the button as the callout view
    annotationView.leftCalloutAccessoryView = myDetailButton;
    annotationView.canShowCallout = YES;
    }
     return annotationView;
  }

这很好用,但用红色图钉向我显示用户位置。 我检查了 IB 中的“显示用户位置”。 我想使用默认的蓝点,当用户移动时它会自动移动,这不正确吗? 我怎样才能做到这一点?

多谢。

I am developing a custom pins in a MapView in my iPhone app.
Here is the code:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id  <MKAnnotation>)annotation
 {
     static NSString *AnnotationViewID = @"annotationViewID";

     MKAnnotationView *annotationView = (MKAnnotationView *)[myMapView  dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

     if (annotationView == nil)
     {
         annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
     }

    if ([[annotation title] isEqualToString:NSLocalizedString(@"Current Location",@"")])  {

        MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
        //pin.pinColor = MKPinAnnotationColorRed;
        annotationView = pin;
    }
    else
    {
        NSString *pin = [NSString stringWithFormat:@"pin%i.png",[[annotation imgNumber] intValue]];
        annotationView.image = [UIImage imageNamed:pin];//Canviar la chincheta per numero
        UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [annotationView setRightCalloutAccessoryView:button];
        annotationView.annotation = annotation;
        annotationView.canShowCallout = YES;

    /********* Imagen a la izquierda en la burbuja *********/
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    // Set up the Left callout
    UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myDetailButton.frame = CGRectMake(0, 0, 23, 23);
    myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    // Set the image for the button
    //[myDetailButton setImage:[UIImage imageNamed:@"botocorreu.png"] forState:UIControlStateNormal];
    NSString *detailButton = [NSString stringWithFormat:@"%i",[[annotation imgNumber] intValue]];
    [myDetailButton setImage:[UIImage imageNamed:detailButton] forState:UIControlStateNormal];  
    // Set the button as the callout view
    annotationView.leftCalloutAccessoryView = myDetailButton;
    annotationView.canShowCallout = YES;
    }
     return annotationView;
  }

This works good but show me the users location with a red pin.
I check the "show user location" in the IB.
i would like to use the default blue point which will move it automatically when the user is moving, isn't correct?
How can i do that?

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染柒℉ 2024-11-15 14:52:52

将其添加到 viewForAnnotation 方法的 top 中:

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

特殊用户位置注释的类型为 MKUserLocation 并在这种情况下返回 nil 告诉地图视图为其绘制默认视图,即蓝点。

您还可以删除这些行,因为不再需要它们:

if ([[annotation title] isEqualToString:NSLocalizedString(@"Current Location",@"")])  {
    MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    //pin.pinColor = MKPinAnnotationColorRed;
    annotationView = pin;
}
else

Add this to the top of the viewForAnnotation method:

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

The special user location annotation is of type MKUserLocation and returning nil in that case tells the map view to draw the default view for it which is the blue dot.

You can also remove these lines since they will no longer be needed:

if ([[annotation title] isEqualToString:NSLocalizedString(@"Current Location",@"")])  {
    MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    //pin.pinColor = MKPinAnnotationColorRed;
    annotationView = pin;
}
else
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文