在iPhone MKMapView中显示用户位置蓝点 [英] show users location blue point in iPhone MKMapView

查看:224
本文介绍了在iPhone MKMapView中显示用户位置蓝点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
  }

这很好但是用红色针脚显示用户位置。
我查看IB中的显示用户位置。
我想使用默认的蓝点,当用户移动时会自动移动它,是不是正确的?
我该怎么做?

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?

非常感谢。

推荐答案

将此添加到 viewForAnnotation 方法的顶部

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

特殊用户位置注释的类型为 MKUserLocation 并且在这种情况下返回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

这篇关于在iPhone MKMapView中显示用户位置蓝点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆