How to implement MKAnnotationViews that rotate using a map

I have an application with some annotations on it, and so far they are just characters that look good with default behavior (like numbers and letters, for example). Their directions are fixed in orientation with a suitable device.

Now, however, I need some annotations that need to be fixed in orientation to the actual map, so if the map rotates, then the annotation symbols must rotate with it (for example, an arrow indicating the flow of a river, for example).

I don’t want them to scale using the map as overlays, but I want them to rotate with the map.

I need a solution that primarily works when the user manually rotates the map with his fingers, as well as when he rotates due to tracking with the header mode.

In Google Maps (at least on Android) it is very simple with the simple MarkerOptions.flat (true)

I am sure that on ios it will not be so difficult.

Thanks in advance!

+4
source share
1 answer

Here is what I used for something like that.

- (void)rotateAnnotationView:(MKAnnotationView *)annotationView toHeading:(double)heading
{
    // Convert mapHeading to 360 degree scale.
    CGFloat mapHeading = self.mapView.camera.heading;
    if (mapHeading < 0) {
        mapHeading = fabs(mapHeading);
    } else if (mapHeading > 0) {
        mapHeading = 360 - mapHeading;
    }

    CGFloat offsetHeading = (heading + mapHeading);
    while (offsetHeading > 360.0) {
        offsetHeading -= 360.0;
    }

    CGFloat headingInRadians = offsetHeading * M_PI / 180;
    annotationView.layer.affineTransform = CGAffineTransformMakeRotation(headingInRadians);
}

And then it's called in regionDidChange, etc.

, , , , , . affineTransform , .

, , , .

+4

Source: https://habr.com/ru/post/1570714/


All Articles