質問

I have MKMapView with many annotations in proper order. And I have button to change map mode:

- (IBAction)userDidPressTrackButton:(id)sender {
    if (self.mapView.userTrackingMode == MKUserTrackingModeFollow) {
        self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
        return;
    }

    if (self.mapView.userTrackingMode == MKUserTrackingModeFollowWithHeading) {
        self.mapView.userTrackingMode = MKUserTrackingModeNone;
        return;
    }

    if (self.mapView.userTrackingMode == MKUserTrackingModeNone) {
        self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    }
}

when mode = MKUserTrackingModeFollowWithHeading annotations begin to put themselves in random order. It seems that in this mode mapview begin to redraw itself every second and put all the subview (annotations) in unknown order.

How to cancel changing order of annotations?

役に立ちましたか?

解決

The array of annotations that you get back from mapview.annotations is not guaranteed to be in the same order you that added them in. If you are relying on them vein in a special order you are probably doing your viewForAnnotations function wrong. It is given an annotation as a parameter, you use that to determine what view to draw. Usually people make a custom class that implements the MKAnnotation protocol and has some addition variables in which they store the data needed to create the right view. When viewForAnnotations gets called they check if the provided annotation is one of their special class, and if so extract the data, make the view and return it. There is no need to know the order of the annotations array, and it wouldn't do you any good because an MKMapView can and will ask for the annotations in any sequence it likes.

Side note: Why are you writing your own function for toggling the tracking mode? You can just use the official one and it will do it all for you.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top