【问题标题】:distinction between user annotation an other annotations用户注释与其他注释之间的区别
【发布时间】:2011-05-05 21:35:57
【问题描述】:

请我最近制作了一张地图,它假设显示用户位置(绿色针颜色)和服务站的其他注释(红色针颜色),目前,我能够显示所有具有相同颜色的注释和我仍然无法告诉MKMapView 代表如何区分两种引脚类型,因此为每种类型分配正确的标题以显示。 这是我的MKAnnotationView 方法:

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

    static NSString *identifier = @"MyLocation";   
    if ([annotation isKindOfClass:[MyLocation class]]) {

        MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        } else {
            annotationView.annotation = annotation;
        }

        annotationView.enabled = YES;
        [annotationView setAnimatesDrop:YES];
        annotationView.canShowCallout = YES;

               return annotationView;
    }

    return nil;    
}

对于我的实现MKAnnotation委托的类,我有这个方法:

-(id)initWithName:(NSString *)enseigneDeLaStation distanceVersLaStation:(NSString *) distanceVersLaStation coordinate:(CLLocationCoordinate2D)coordinate
{
    if ((self = [super init])) {
        _enseigneDeLaStation = [enseigneDeLaStation copy];
        _distanceVersLaStation = [distanceVersLaStation copy];
        _coordinate = coordinate;
    }
    return self;
}

提前感谢您的帮助:)

编辑 我尝试像这样制作用户位置注释,如果我错了,请纠正我:

location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
    MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
    [mapView addAnnotation:annotation];

编辑 2 再次嗨,我尝试制作一个注释以确保它甚至调用注释方法,所以我制作了这个简单的例子:

latitudeOfUserLocation=43.2923;
    longitudeOfUserLocation=5.45427;
    location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
    MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"coucou" coordinate:location2D]autorelease];
    [mapView addAnnotation:annotation];

    MKCoordinateSpan span={latitudeDelta:1,longitudeDelta:0.5};
    MKCoordinateRegion region={location2D,span};
    [mapView setRegion:region];

当我运行应用程序时,我看到与 43.2923/5.45427 一致的区域缩放得很好,但我没有看到注释,为什么它没有调用注释方法,我无法继续,因为它没有显示用户注释,请帮助,提前谢谢:)

编辑 3 嗨,我认为这总是与用户和站点引脚之间的差异有关,所以我声明了一个 var codeColor,在调用注释时将其设置为 1(用户的类型)并将其设置为 2(站点type) 调用车站的注释时:

-(void)viewWillAppear:(BOOL)animated
{  
       codeColor=1;//it's the first type, the user one
    MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are right here"   distanceVersLaStation:@"" coordinate:location2D]autorelease];
    annotation.pinColor = MKPinAnnotationColorGreen;  
    [mapView addAnnotation:annotation];

}

和:

-(void)requestFinished:(ASIHTTPRequest *)request
{  codeColor=2;
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    MyLocation *annotation=[[[MyLocation alloc]initWithName:ensStation distanceVersLaStation:distance coordinate:location2D]autorelease];
           annotation.pinColor = MKPinAnnotationColorPurple;  
           [mapView addAnnotation:annotation];


           }

对于注解方法:

if (codeColor==2) {

            annotationView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        }

现在第一次测试时,一切正常,用户注释没有按钮,站一个是按钮,但是当我从视图中移动回来时,所有注释都带有按钮,甚至用户一。你能帮我吗,谢谢提前:)

【问题讨论】:

  • 如何添加用户位置注释?您要为其显示绿色图钉还是标准动画蓝点?
  • 嗨,实际上我想显示 API 提供的绿色

标签: ios annotations mapkit


【解决方案1】:

一种简单的方法是将 pinColor 属性添加到 MyLocation 类:

@property (nonatomic, assign) MKPinAnnotationColor pinColor;

创建注解时,设置pinColor:

MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
annotation.pinColor = MKPinAnnotationColorGreen;  //or red or whatever
[mapView addAnnotation:annotation];

在viewForAnnotation方法中:

//...
annotationView.canShowCallout = YES;
annotationView.pinColor = ((MyLocation *)annotation).pinColor;

【讨论】:

  • 嗨安娜,你的想法看起来不错,但没有任何改变,所有注释都有相同的颜色,用户注释的标题也没有改变(“你在这里”)..无论如何,我仍在努力,我正在遵循你的想法
  • 嗨安娜,我注意到用户的注释甚至没有显示,尽管我为用户和电台注释制作了相同的代码,请查看我的第二次编辑,谢谢:)
  • 代码看起来不错。您确定 addAnnotation 行正在执行(在此处放置断点)吗?自从它开始工作以来,你做了哪些改变?
  • 嗨,我在该行设置了一个断点:[mapView addAnnotation:annotation];,我在控制台中得到了它:Pending breakpoint 1 - ""StationsSurLaCarteViewController.m":102" resolved 这是否意味着它可以??
  • 该行是否在代码编辑器中以紫色突出显示?好的。点击 Continue 或从菜单 Run|Continue 完成应用程序的运行。
猜你喜欢
  • 2012-06-25
  • 2015-05-08
  • 2018-07-04
  • 1970-01-01
  • 2014-10-08
  • 2010-10-15
  • 2016-12-28
  • 2017-07-05
  • 1970-01-01
相关资源
最近更新 更多