检测选定的注释以更改图钉颜色 [英] Detecting selected annotation to change pin color

查看:79
本文介绍了检测选定的注释以更改图钉颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用mapView.这是场景:

I am now working on a mapView. This is the scenario:

  1. 所有注释均来自JSON对象(MySQL表).
  2. 注释按源表分组:
  3. 表1-> Ofertas,表2->光标,表3-> Eventos.
  4. 最初显示的地图区域足够大,可以稍后显示所有注释.
  5. 有一个UISegmentedControll,可让用户选择应显示的注释组.

在此应用程序状态下,我需要进行以下更新:

At this app state, I will need to make the following updates:

  1. 每个注释组应使用不同的图钉颜色.

这是我用来根据所选索引将注释绘制到mapView的方法:

This is the method I am using to draw the annotations to the mapView depending on the selected index:

- (IBAction)changeOpcion:(id)sender{

    if(miControl.selectedSegmentIndex == 0)
    {
        //[mapView_ clear];

        [self removeAllPinsButUserLocation2];
        NSLog(@"********0");
        for ( int i=0;i<[categorias count];i++){

            int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue];



            if (grupo == 1){

                double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue];

                double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue];

                CLLocationCoordinate2D lugar;
                lugar.latitude = latitud;
                lugar.longitude = longitud;

                NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"];


                NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"direccion"];

                CLLocationCoordinate2D coordinate3;
                coordinate3.latitude = latitud;
                coordinate3.longitude = longitud;
                myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre];
                [self.mapView addAnnotation:annotation3];

            }
        }
    }
    else if(miControl.selectedSegmentIndex == 1)
    {
        [self removeAllPinsButUserLocation2];

        for ( int i=0;i<[categorias count];i++){

            int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue];



            if (grupo == 2){

                double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue];

                double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue];

                CLLocationCoordinate2D lugar;
                lugar.latitude = latitud;
                lugar.longitude = longitud;

                NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"];


                NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"direccion"];


                CLLocationCoordinate2D coordinate3;
                coordinate3.latitude = latitud;
                coordinate3.longitude = longitud;
                myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre];
                [self.mapView addAnnotation:annotation3];


            }
        }

        //action for the second button
    }
    else if(miControl.selectedSegmentIndex == 2)
    {
        [self removeAllPinsButUserLocation2];

        for ( int i=0;i<[categorias count];i++){

            int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue];



            if (grupo == 3){

                double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue];

                double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue];

                CLLocationCoordinate2D lugar;
                lugar.latitude = latitud;
                lugar.longitude = longitud;

                NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"];


                NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"direccion"];


                CLLocationCoordinate2D coordinate3;
                coordinate3.latitude = latitud;
                coordinate3.longitude = longitud;
                myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre];
                [self.mapView addAnnotation:annotation3];


            }
        }
    }


}

这是我的viewForAnnotation方法的代码:

And this is my code for the viewForAnnotation method:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {

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


    static NSString *identifier = @"myAnnotation";
    MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annotationView)
    {

        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.pinColor = MKPinAnnotationColorPurple;
        annotationView.animatesDrop = YES;
        annotationView.canShowCallout = YES;
    }else {
        annotationView.annotation = annotation;
    }
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return annotationView;
}

我的第一个问题是:如何更新viewForAnnotation方法以检测所选注释的组以更改图钉颜色.

My first question is: how to update the viewForAnnotation method to detect the group of the selected annotation to change the pin colour.

推荐答案

由于您一次只显示一组(一种颜色)注释,因此最简单,最直接(但 crude -不推荐)的方法是:

Since you're only showing one group (one color) of annotations at a time, the simplest, most direct (but crude -- not recommended) approach is:

viewForAnnotation中,基于miControl.selectedSegmentIndex的当前值设置pinColor.

In viewForAnnotation, set pinColor based on the current value of miControl.selectedSegmentIndex.

仅当您一次显示一组注释并且将分段控件的值(某些外部UI元素)与地图委托方法绑定在一起时,此方法才有效.将来,您可能希望一次显示多个组,或者可能想要更改用于控制显示哪些组的UI,现在必须更改viewForAnnotation等中的代码.

This will only work right if you show one group of annotations at a time and it ties the value of the segmented control (some external UI element) with the map delegate method. In the future, you may want to show multiple groups at once or you may want to change the UI that controls what groups are shown and now you have to change the code in viewForAnnotation, etc.


请注意,在viewForAnnotation委托方法中,它将引用传递给地图要为其查看视图的注释对象(annotation参数).


Notice that in the viewForAnnotation delegate method, it passes a reference to the annotation object that the map wants the view for (the annotation parameter).

如果注释对象本身知道它属于哪个组,则可以基于annotation的某些属性在viewForAnnotation中设置pinColor.

If the annotation object itself knew what group it belonged to, then you could set the pinColor in viewForAnnotation based on some property of annotation.

因此,推荐的方法是将所需的数据添加到注释对象本身(到您的myAnnotation类中).您可以向其中添加grupo属性,在创建每个注释时设置此属性,然后在viewForAnnotation中对其进行检查以设置pinColor.例如:

So the recommended approach is to add the data needed to the annotation object itself (to your myAnnotation class). You could add a grupo property to it, set this property when you create each annotation, then check it in viewForAnnotation to set the pinColor. For example:

  • myAnnotation.h 中:

@property (nonatomic, assign) int grupo;

  • changeOpcion:方法中,在其中添加注释:

  • In the changeOpcion: method where you add an annotation:

    myAnnotation *annotation3 = [[myAnnotation alloc] initWith...
    annotation3.grupo = grupo;  // <-- tell the annotation what group it's in
    [self.mapView addAnnotation:annotation3];
    

  • 最后,在返回视图之前,在viewForAnnotation中,根据grupo设置pinColor:

  • Finally, in viewForAnnotation, right before returning the view, set the pinColor based on grupo:

    if ([annotation isKindOfClass:[myAnnotation class]])
    {
        myAnnotation *myAnn = (myAnnotation *)annotation;
        switch (myAnn.grupo)
        {
            case 1:
                annotationView.pinColor = MKPinAnnotationColorGreen;
                break;
            case 2:
                annotationView.pinColor = MKPinAnnotationColorPurple;
                break;
            default:
                annotationView.pinColor = MKPinAnnotationColorRed;
                break;
        }
    }
    
    return annotationView;
    

  • 这样,您可以一次显示多个或所有组(颜色),而viewForAnnotation不依赖于外部UI.

    This way, you could show multiple or all groups (colors) at once and the viewForAnnotation doesn't rely on the external UI.



    只需评论与您的​​问题无关的代码即可
    changeOpcion:方法中,在注释之间循环的代码没有不必要的重复.这三个块之间的唯一区别是正在检查的grupo的值(if (grupo == 1)if (grupo == 2)if (grupo == 3)).



    Just a comment on the code unrelated to your question or problem:
    In the changeOpcion: method, there is unnecessary duplication of code where it loops through the annotations. The only difference between the three blocks is the value of grupo that it is checking (if (grupo == 1), if (grupo == 2), if (grupo == 3)).

    一个替代方法是创建一个以grupo作为参数的方法,然后从changeOpcion:调用该方法.示例:

    One alternative is to create a method that has grupo as a parameter and then call that method from changeOpcion:. Example:

    - (void)addAnnotationsForGroup:(int)selectedGrupo
    {
        [self removeAllPinsButUserLocation2];
        NSLog(@"********0");
    
        for (int i=0; i < [categorias count]; i++) 
        {    
            int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue];
    
            if (grupo == selectedGrupo)
            {
                //code to create annotation here
            }
        }
    }
    
    - (IBAction)changeOpcion:(id)sender
    {
        switch (miControl.selectedSegmentIndex) 
        {
            case 0:
                [self addAnnotationsForGroup:1];
                break;
            case 1:
                [self addAnnotationsForGroup:2];
                break;
            case 2:
                [self addAnnotationsForGroup:3];
                break;
            default:
                break;
        }
    }
    

    这篇关于检测选定的注释以更改图钉颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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