shiznatix
shiznatix

Reputation: 1107

iOS set MKMapView camera heading rotation around anchor point

I have a MKMapView that I am trying to rotate around the bottom center of itself. I have a simple method that gets the users magnetometer heading and changes the camera view of the map to the heading, this works just fine.

My problem though is setting the anchor point on the map in lat/long that the map will rotate around. I can get set my desired point to be the bottom of my map with this method:

-(void)setMapCenterFromLocation:(CLLocationCoordinate2D)location {
    MKCoordinateRegion oldRegion = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(location, 10000, 10000)];
    CLLocationCoordinate2D centerPointOfOldRegion = oldRegion.center;

    CLLocationCoordinate2D centerPointOfNewRegion = CLLocationCoordinate2DMake(centerPointOfOldRegion.latitude + oldRegion.span.latitudeDelta / 2.0, centerPointOfOldRegion.longitude);

    MKCoordinateRegion newRegion = MKCoordinateRegionMake(centerPointOfNewRegion, oldRegion.span);

    [self.mapView setRegion:newRegion animated:YES];
}

And I can rotate the map with:

- (void)magneticDirectionChanged:(NSNotification *)notification {
    CLHeading *heading = (CLHeading *)[[notification userInfo] valueForKey:@"heading"];

    CLLocationDirection newDirection = heading.trueHeading;

    self.currentHeading = (int)newDirection;

    [self.mapView.camera setHeading:self.currentHeading];
}

But the rotation happens around the center of the map, not the bottom center point that I wanted. Even if I call setMapCenterFromLocation: after doing the rotation, it still counts it as looking from north and thus it goes off to the sides of the map.

How do I keep a specific point on the bottom center of the map while programmatically rotating the map / resizing the map?

Edit: A solution I suppose would be to double the height of the map and then hide the bottom half of the map from being visible. How could this be accomplished?

Upvotes: 1

Views: 1872

Answers (1)

GonzoCoder
GonzoCoder

Reputation: 259

First, in Interface Builder, stick the map view in a scroll view. Then set the height on the map view to something like 1000. The center of the map view will be on the bottom of the screen. This worked with zero auto layout constraints on the view controller.

The key for me was the scroll view part. Up until I added it, I couldn't get the map view to draw any higher than 568 (although larger widths worked), no matter what I set it to in IB. I guess the scroll view forces past whatever is restricting the height.

Upvotes: 2

Related Questions