Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取MapView.annotations数组中MKAnnotation的索引_Ios_Xcode_Indexing_Mkmapview_Mkannotation - Fatal编程技术网

Ios 获取MapView.annotations数组中MKAnnotation的索引

Ios 获取MapView.annotations数组中MKAnnotation的索引,ios,xcode,indexing,mkmapview,mkannotation,Ios,Xcode,Indexing,Mkmapview,Mkannotation,我正在用xCode制作一个带有MKMapView和MKAnnotations的应用程序。如果您做了两个以上的注释,则额外的航路点颜色(PinAnnotations)应更改为紫色 因此,我需要注释中的标记、IndexPath或ID之类的东西来标识MKAnnotation函数中的MKAnnotation。我使用了这行代码: - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotat

我正在用xCode制作一个带有MKMapView和MKAnnotations的应用程序。如果您做了两个以上的注释,则额外的航路点颜色(PinAnnotations)应更改为紫色

因此,我需要注释中的标记、IndexPath或ID之类的东西来标识MKAnnotation函数中的MKAnnotation。我使用了这行代码:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;

    if (annotation != mkMap.userLocation)
    {
        static NSString *defaultPinID = @"aPin";
        pinView = (MKPinAnnotationView *)[mkMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

        if (pinView == nil)
        {
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];

            pinView.canShowCallout = YES;

            pinView.tag = mapView.annotations.count;    // tag is not available for annotation in this function
        }
    }

    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;
    pinView.draggable = YES;


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

    NSLog(@"Annotation-Index: %d", [mapView.annotations indexOfObject:annotation]);
    NSLog(@"MapView.annotations.count = %d", mapView.annotations.count);

    if (1 == [mapView.annotations indexOfObject:annotation])
        pinView.pinColor = MKPinAnnotationColorGreen;
    else if (1 < [mapView.annotations indexOfObject:annotation])
    {

        pinView.pinColor = MKPinAnnotationColorRed;

        // checkes for extra waypoints
        if (2 < mapView.annotations.count)
        {
            for (int i = 2; i > mapView.annotations.count; i++)
            {
                MKPinAnnotationView *aView = [mapView.annotations objectAtIndex:i];
                aView.pinColor = MKPinAnnotationColorPurple;

                [mapView removeAnnotation:mapView.annotations[i]];
                NSMutableArray *a = [[NSMutableArray alloc] init];
                [a replaceObjectAtIndex:i withObject:aView];

                [mapView addAnnotations:a];
            }
        }
    }

    return pinView;
}
但是这个函数的输出(注释索引)让我印象深刻。有时一切正常,但注释索引的值正确,但大多数情况下,这些值似乎是随机生成的,如果MapView.annotations.count仅为3,则比例从0到10


致以最良好的问候和感谢

处理此问题的建议方法是创建自己的类,该类实现MKAnnotation协议,并具有一个属性,您可以在
viewForAnnotations
期间检查该属性以查看要使用的颜色。MKMapView中的
annotations
数组不保证按照向地图添加注释的顺序。你可以加上烦扰,烦扰,然后烦扰,但你可以得到回复[anno2,anno3,anno1]。事情就是这样,你不能改变。因此,您可以保留自己的注释数组,这些注释不会被重新排列。如果合适的话,也可以使用额外的财产概念


您不应该做的一件事是在
viewForAnnotation
功能中添加更多注释,这真是一团糟。

您可能可以打印注释的标题,以便更好地了解发生了什么。(标题为
view.annotation
和所有
mapView.annotations
)您知道,当用户单击阵列时,您的代码仅在阵列上插入注释吗?来自Apple docs:mapView:annotationView:calloutAccessoryControlTapped:告诉代理用户点击了批注视图的一个附件按钮。抱歉,伙计们,我输入了一个错误的示例,注意到它太晚了,但现在我编辑了它。。。但是你是对的,我会试试它的声音很好,非常感谢你的详细回答!我会试试看,如果行得通的话给你打勾!很好用!非常感谢。如果有像我这样的人对自定义MKAnnotation类有问题,这里有一个链接(非常简单):
int AnnotationIndex = [mapView.annotations indexOfObject:annotation];