我在mkMapView中更改细分时无法更改图钉颜色 [英] Can't be changed pin color when I change segement in mkMapView

查看:93
本文介绍了我在mkMapView中更改细分时无法更改图钉颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对mapView中的注释存在一些问题.让我们看一下我的要求.

I have some problem regarding Annotations in mapView. Lets have one glance on my requirement.

我想让用户选择会议地点.

I want to give choice to user to choose location for meeting.

有两个选项.

1)我应该按数据提供近距离列表

1) I should give list of near by data

2)他可以将销钉拖放到任何想要的地方!

2) He can drag and drop pin anywhere he wants !

为此,我创建了一个细分. 数据附近的第一个索引 和 掉针的第二个索引.

For that I have created one segment. First index for near by data and Second index for dropping a pin.

对于第一个选项(附近" ),我需要从卖方位置,买方位置以及卖方与买方之间的中点获取数据.所以我叫谷歌api并通过传递纬度和经度三次来获取数据.第一次获取数据没有问题.我的阵列填充了所有数据(包括3个响应),并且针脚颜色也根据要求进行了更改.

For First option ("near by") I need to fetch near by data from location of Seller, location of Buyer and midpoint between seller and buyer. So I call google api and get data by passing latitude and longitude three times. There is no issue when I get data first time. My array fill up with all data (included 3 responses) and pin color also changes as per requirement.

买方(红色) 卖家(紫色) 中点(绿色)

Buyer (Red Color) Seller ( Purple) Mid Point (Green)

现在,当我单击放置图钉时,所有数据将从阵列中删除,并且一个图钉被放置在地图上.

Now when I click on drop pin all data are removed from array and one pin is dropped on map.

到现在为止一切正常!

但是当您再次单击"near by"时,问题开始了!毫无疑问,它可以为我提供所需的数据,但不会保留图钉颜色.

But when you again click on "near by", Problem starts ! No doubt it gives me data as I want but pin colors don't maintained.

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

    if ([segmentND selectedSegmentIndex]==0) {

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

       static NSString* BridgeAnnotationIdentifier = @"bridgeAnnotationIdentifier";
        MKPinAnnotationView* pinView = (MKPinAnnotationView *)
        [myMapView dequeueReusableAnnotationViewWithIdentifier:BridgeAnnotationIdentifier];
        if (!pinView)
        {
            MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                                   initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease];

            switch (self.pinColor) {
                case 0:
                {
                    customPinView.pinColor = MKPinAnnotationColorPurple;
                }
                    break;
                case 1:
                {
                    customPinView.pinColor = MKPinAnnotationColorRed;
                }
                    break;
                case 2:
                {
                    customPinView.pinColor = MKPinAnnotationColorGreen;
                }
                    break;
                default:
                    break;
            }
            customPinView.canShowCallout = YES;

            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];


            [rightButton addTarget:self
                            action:@selector(showDetails:)
                  forControlEvents:UIControlEventTouchUpInside];
            customPinView.rightCalloutAccessoryView = rightButton;

            return customPinView;
        }
        else
        {
            pinView.annotation = annotation;
        }
        return pinView;
    }
    else {

        // Code of dragging dropping pin. It works Fine.s
    }
}

我附上图像,以了解更多想法.

I am attaching image for more idea.

请给我解决方案或其他实现方法. 必须记住图钉颜色以区分卖方买方和中点!

Please give me solution or any another way to implement it. Remember Pin color is compulsory to differentiate Seller Buyer and Midpoint !

推荐答案

当前方法的问题在于self.pinColor不会根据地图需要查看的注释而改变.它可以并且将在需要时调用viewForAnnotation.也许地图已经滚动了,只有一个图钉重新出现了.也许该应用程序已被置于后台,并且正被用户重新带入视图.您需要分析批注的原因是什么,以确定在视图中使用哪种图钉颜色.您要使用什么对象做注释?如果它是HSAnno,并且具有名为pinColor的属性,则可以执行以下操作,而不是执行switch语句.

The trouble with your current approach is that self.pinColor doesn't change according to which annotation the map is needing a view for. It can and will call viewForAnnotation when ever it feels like it. Maybe the map has been scrolled around and one pin has just come back into view. Maybe the app was put into the background and is just being brought back into view by the user. What ever the reason you need to analyze the annotation is is passing in to determine which pin colour to use in the view. What object are you using for your annotation? If it was HSAnno and it had a property called pinColor you'd do something like this instead of your switch statement.

HSAnno* currentAnno = (HSAnno *)annotation;
pinView.pinColor = currentAnno.pinColor;

这样,无论需要重绘什么批注,viewForAnnotation都将始终返回正确的彩色图钉.

That way no matter what annotation needed to be redrawn viewForAnnotation would always return the right coloured pin.

这篇关于我在mkMapView中更改细分时无法更改图钉颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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