I am using a MKUserTrackingBarButtonItem
button to allow the user to automatically track their location on a map. The problem is that when they tap this button, it is zoomed too far out. I want it to start at a specified zoom level (i.e. span). How can I achieve this?
我正在使用MKUserTrackingBarButtonItem按钮允许用户自动跟踪他们在地图上的位置。问题是,当他们点击此按钮时,它会被缩放得太远。我希望它以指定的缩放级别(即跨度)开始。我怎样才能做到这一点?
When the user taps the button to change to MKUserTrackingModeFollow
, it seems to use the same zoom level that the user last manually changed to (i.e. using gestures on the map). Attempting to specify a different zoom level via setRegion
or setVisibleMapRect
does not affect what zoom level will be used when the mode is changed to MKUserTrackingModeFollow
.
当用户点击按钮以改变为MKUserTrackingModeFollow时,它似乎使用用户最后手动改变的相同缩放级别(即,在地图上使用手势)。尝试通过setRegion或setVisibleMapRect指定不同的缩放级别不会影响将模式更改为MKUserTrackingModeFollow时将使用的缩放级别。
Attempting to override mapView:didChangeUserTrackingMode:
to set the region causes the mode to be changed back to MKUserTrackingModeNone
. Example:
尝试覆盖mapView:didChangeUserTrackingMode:设置区域会导致模式更改回MKUserTrackingModeNone。例:
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated {
if (mode == MKUserTrackingModeFollow) {
CLLocationCoordinate2D center = mapView.userLocation.location.coordinate;
MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717);
[mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];
// [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:NO];
}
}
If I attempt to reset the mode immediately after setting the region, it works fine if the user is stationary, but zooms back out if the user is moving.
如果我在设置区域后立即尝试重置模式,如果用户静止则可以正常工作,但如果用户正在移动则缩小。
The simplest solution would be if there was a way to simply specify something like a zoom level for MKUserTraking by sending it my span value. However, since that doesn't seem to exist, what else can I do?
最简单的解决方案是,如果有一种方法可以通过发送我的跨度值来简单地为MKUserTraking指定类似缩放级别的内容。但是,由于这似乎不存在,我还能做什么?
2 个解决方案
#1
7
I had the same issue and used a different approach to fix it. You can use the MapCamera function for this instead of that button.
我有同样的问题,并使用不同的方法来解决它。您可以使用MapCamera函数代替该按钮。
On each new location do this:
在每个新位置执行此操作:
MKMapCamera *newCamera = [MKMapCamera cameraLookingAtCenterCoordinate:[newLocation coordinate]
fromEyeCoordinate:[oldLocation coordinate]
eyeAltitude:2000];
[mapView setCamera:newCamera animated:TRUE];
And play with the eyeAltitude.
并且用眼睛玩耍。
If the user manually zooms in or out you can read the altitude value from mapview.camera.altitude also don't update the camera when the user is manually using the map.
如果用户手动放大或缩小,您可以从mapview.camera.altitude读取高度值,当用户手动使用地图时也不要更新相机。
#2
1
According to apple documentation used here
根据这里使用的苹果文档
https://developer.apple.com/reference/mapkit/mkmapview/1616208-usertrackingmode
https://developer.apple.com/reference/mapkit/mkmapview/1616208-usertrackingmode
Setting the tracking mode to follow or followWithHeading causes the map view to center the map on that location and begin tracking the user’s location. If the map is zoomed out, the map view automatically zooms in on the user’s location, effectively changing the current visible region.
将跟踪模式设置为跟随或跟随使用标题会导致地图视图将地图置于该位置的中心,并开始跟踪用户的位置。如果地图缩小,地图视图会自动放大用户的位置,从而有效地更改当前可见区域。
Here changing the region does not effect your visible region due to that reason.
由于这个原因,更改区域不会影响您的可见区域。
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated {
if (mode == MKUserTrackingModeFollow) {
CLLocationCoordinate2D center = mapView.userLocation.location.coordinate;
MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717);
[mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];
// [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:NO];
}
}
So you just need to change center coordinate on didChangeUserTrackingMode
instead of changing the whole region
因此,您只需要更改didChangeUserTrackingMode上的中心坐标,而不是更改整个区域
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated {
if (mode == MKUserTrackingModeFollow) {
[self.mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
}
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
[self.mapView setCenterCoordinate:mapViewuserLocation.location.coordinate animated:YES];
}
on click of MKUserTrackingBarButtonItem change the zoom level
点击MKUserTrackingBarButtonItem更改缩放级别
CLLocationCoordinate2D center = mapView.userLocation.location.coordinate;
MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717);
[mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];
#1
7
I had the same issue and used a different approach to fix it. You can use the MapCamera function for this instead of that button.
我有同样的问题,并使用不同的方法来解决它。您可以使用MapCamera函数代替该按钮。
On each new location do this:
在每个新位置执行此操作:
MKMapCamera *newCamera = [MKMapCamera cameraLookingAtCenterCoordinate:[newLocation coordinate]
fromEyeCoordinate:[oldLocation coordinate]
eyeAltitude:2000];
[mapView setCamera:newCamera animated:TRUE];
And play with the eyeAltitude.
并且用眼睛玩耍。
If the user manually zooms in or out you can read the altitude value from mapview.camera.altitude also don't update the camera when the user is manually using the map.
如果用户手动放大或缩小,您可以从mapview.camera.altitude读取高度值,当用户手动使用地图时也不要更新相机。
#2
1
According to apple documentation used here
根据这里使用的苹果文档
https://developer.apple.com/reference/mapkit/mkmapview/1616208-usertrackingmode
https://developer.apple.com/reference/mapkit/mkmapview/1616208-usertrackingmode
Setting the tracking mode to follow or followWithHeading causes the map view to center the map on that location and begin tracking the user’s location. If the map is zoomed out, the map view automatically zooms in on the user’s location, effectively changing the current visible region.
将跟踪模式设置为跟随或跟随使用标题会导致地图视图将地图置于该位置的中心,并开始跟踪用户的位置。如果地图缩小,地图视图会自动放大用户的位置,从而有效地更改当前可见区域。
Here changing the region does not effect your visible region due to that reason.
由于这个原因,更改区域不会影响您的可见区域。
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated {
if (mode == MKUserTrackingModeFollow) {
CLLocationCoordinate2D center = mapView.userLocation.location.coordinate;
MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717);
[mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];
// [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:NO];
}
}
So you just need to change center coordinate on didChangeUserTrackingMode
instead of changing the whole region
因此,您只需要更改didChangeUserTrackingMode上的中心坐标,而不是更改整个区域
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated {
if (mode == MKUserTrackingModeFollow) {
[self.mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
}
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
[self.mapView setCenterCoordinate:mapViewuserLocation.location.coordinate animated:YES];
}
on click of MKUserTrackingBarButtonItem change the zoom level
点击MKUserTrackingBarButtonItem更改缩放级别
CLLocationCoordinate2D center = mapView.userLocation.location.coordinate;
MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717);
[mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];