Domanda

I am making an App in xCode with an MKMapView and MKAnnotations. If you make more than two annotations the extra waypoints colors (PinAnnotations) should be changed to purple.

Therefore I need something like a tag, IndexPath or ID from the annotation to identify the MKAnnotation in the MKAnnotation functions. I used this line of code:

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

I already google this question and found a number of solutions like I did

int AnnotationIndex = [mapView.annotations indexOfObject:annotation];

but the the output of this function (Annotation-Index) strikes my mind. Sometimes everything is fine but and the Annotation-Index has the right value but most of the time the values seems to be generated randomly and the scale goes from 0 up to 10 also if the MapView.annotations.count is just 3!

Best regards and thanks!

È stato utile?

Soluzione

The recommended way of handling this is to make your own class that implements the MKAnnotation protocol and has a property that you can check during viewForAnnotations to see what colour to use. The annotations array from MKMapView is not guaranteed to be in the order in which you add annotations to the map. You may add annoy, annoy and then annoy but you may get back [anno2, anno3, anno1]. That's just the way it is and you can't change that. So, you could keep your own array of annotations that won't get rearranged. Or use the extra property idea if that will suit.

One thing you should not do is add more annotations during the viewForAnnotation function, that's really messed up.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top