用户注释和其他注释之间的区别

发布于 2024-11-05 02:12:09 字数 3686 浏览 1 评论 0原文

请我最近制作了一张地图,它应该显示用户位置(绿色引脚颜色)和服务站的其他注释(红色引脚颜色),目前,我能够以相同的颜色显示所有注释,但我仍然无法告诉 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(站类型):

-(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];

        }

现在第一次测试时,一切正常,用户注释没有按钮,而站注释有按钮,但是当我离开视图并返回时,所有注释都带有按钮,甚至是用户注释。您能帮我一下吗,提前谢谢:)

please i have recently made a Map which suppose to show the user location (green pin color) and others annotations for service stations (red pins color), for the moment, i am able to show all annotations with the same color and i still unable to tell the MKMapView delegate how to make difference between two pin types and so assign to each type the right title to show.
this is my MKAnnotationView method :

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

and for my class which implement the MKAnnotation delegate i have this method :

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

thx in advance for your help :)

EDIT
i try to make the user location annotation like this, please if i am wrong correct me :

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

EDIT 2
Hi again, i try to make one annotation to make sure it even call the annotation method so imake this simple example :

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

when i run the app, i see the region zoomed well which go with the 43.2923/5.45427 but i don't see the annotation, why it's not calling the annotation method,i can't move on since it's not showing the user annotation, please help, thx in advance :)

EDIT 3
Hi, i assumed that this has always relation with making difference between user and station pins, so i have declared a var codeColor, set it to 1 (the type of the user) when invoking the annotations and set it to 2(station type) when calling the annotations for the stations :

-(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];

}

and :

-(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];


           }

and for the annotations method :

if (codeColor==2) {

            annotationView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        }

now when testing for the first time, all is ok, the user annotation without button and the station one is with the button, but when i move from the view and i came back, all annotations are with buttons, even the user one. could you please help me there, thx in advance :)

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

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

发布评论

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

评论(1

携余温的黄昏 2024-11-12 02:12:09

一种简单的方法是将 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;

One simple way is to add a pinColor property to your MyLocation class:

@property (nonatomic, assign) MKPinAnnotationColor pinColor;

When creating the annotation, set the pinColor:

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

and in the viewForAnnotation method:

//...
annotationView.canShowCallout = YES;
annotationView.pinColor = ((MyLocation *)annotation).pinColor;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文