我有一个使用UIViewController
的{{1}}:
这是我的viewDidLoad
MapKit
这是委托方法-(void)viewDidLoad {
[super viewDidLoad];
EventsmapMapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
EventsmapMapView.showsUserLocation = YES;
[EventsmapMapView setMapType:MKMapTypeStandard];
[EventsmapMapView setZoomEnabled:YES];
[EventsmapMapView setScrollEnabled:YES];
}
:
didUpdateUserLocation
基本上,我的问题是,在加载视图时,我可以在地图上定位自己,但是随后我无法在地图上移动。发生的情况是,每次我四处走动时,地图都会自动重新定位我。我知道问题出在-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 5000, 5000);
[self.EventsmapMapView setRegion:[self.EventsmapMapView regionThatFits:region] animated:YES];
}
中,但是我不确定如何修改代码以防止此行为。可以肯定,这是相对简单的事情。
答案 0 :(得分:0)
在didUpdateUserLocation:
中,您呼叫了setRegion:animated:
,这将由于您不在移动而将可见区域更改为当前位置。然后您最终被锁定了。
您可以考虑采用以下方法(未经测试):
首先,定义一个摇摄状态标志
BOOL isMapPanning;
初始化一个平移手势识别器,目标指向您的视图
panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[EventsmapMapView addGestureRecognizer:panGesture];
还有手势处理程序
-(void)handlePanGesture:(UIPanGestureRecognizer*)sender {
switch (sender.state) {
case UIGestureRecognizerStateBegan:
// Stop map updating...
isMapPanning = YES;
break;
case UIGestureRecognizerStateChanged:
break;
case UIGestureRecognizerStateEnded:
// ... until panning is stop
isMapPanning = NO;
break;
default:
break;
}
}
现在,每当CLLocationManager
呼叫您的didUpdateUserLocation
代表时,只需在执行所有操作之前检查平移标志即可。
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
if (!isMapPanning) {
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 5000, 5000);
[self.EventsmapMapView setRegion:[self.EventsmapMapView regionThatFits:region] animated:YES];
}
}
答案 1 :(得分:0)
定义isInitialized
属性。
@property (nonatomic, assign) BOOL isInitialized;
添加警卫条款
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
if (self.isInitialized) {
return;
}
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 5000, 5000);
[self.EventsmapMapView setRegion:[self.EventsmapMapView regionThatFits:region] animated:YES];
self.isInitialized = YES;
}