我创建了一个mapkit应用程序,在此显示我的位置并每次更新此位置并将此位置设置在视图中心。我更新了我的位置并使用布尔变量设置在中心。 我想在我的地图上检测滚动或缩放触摸手指,当我这样做时,我的布尔变量发生了变化,并且没有将我的位置设置在中心但我不知道在mapkit上处理触摸事件。
请指导我并告诉我如何在触摸和滚动地图时更改我的变量。 谢谢。 这是我的代码:
@implementation ViewController
{
CLLocationManager *locationManager;
double latitudes;
double longitudes;
BOOL updateLocation;
}
@synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
updateLocation = YES;
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
mapView.mapType = MKMapTypeStandard;
mapView.zoomEnabled = YES;
mapView.scrollEnabled = YES;
mapView.showsUserLocation = YES;
[mapView.userLocation setTitle:@"I'm Here"];
[self.view addSubview:mapView];
mapView.delegate = self;
[self GetMyLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitudes = currentLocation.coordinate.longitude;
latitudes = currentLocation.coordinate.latitude;
//NSLog(@"%f,%f",longitudes,latitudes);
//[self centerLocation];
}
}
#pragma mark - MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
if (updateLocation) {
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:NO];
}
else{NSLog(@"Don't Update");}
}
答案 0 :(得分:3)
只需使用下面的MKMapView
委托方法
– mapView:regionWillChangeAnimated:
– mapView:regionDidChangeAnimated:
答案 1 :(得分:2)
使用:
mapView:regionWillChangeAnimated:
告诉代表地图视图显示的区域即将更改。
只要当前显示的地图区域,就会调用此方法 变化。在滚动期间,可以多次调用此方法 报告地图位置的更新。因此,您的实施 这种方法应尽量轻巧,以免影响 滚动表现。
答案 2 :(得分:0)
在 Swift 中,您应该使用以下代码
extension UIViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {}
}