iOS set MKMapView camera heading rotation around anchor point

和自甴很熟 提交于 2019-12-20 04:55:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21453199/ios-set-mkmapview-camera-heading-rotation-around-anchor-point

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!