use*_*133 2 objective-c touch uitouch mapkit ios
我创建了一个 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");}
}
Run Code Online (Sandbox Code Playgroud)
使用:
mapView:regionWillChangeAnimated:
Run Code Online (Sandbox Code Playgroud)
它告诉委托者地图视图显示的区域即将更改。
每当当前显示的地图区域发生变化时,就会调用此方法。在滚动期间,可能会多次调用此方法来报告地图位置的更新。因此,您对该方法的实现应该尽可能轻量级,以避免影响滚动性能。
只需使用MKMapView下面的委托方法
\xe2\x80\x93 mapView:regionWillChangeAnimated:\n\xe2\x80\x93 mapView:regionDidChangeAnimated:\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2603 次 |
| 最近记录: |