gpt4 book ai didi

ios - 如何通过触摸检测滚动或缩放 mapkit

转载 作者:行者123 更新时间:2023-11-28 18:33:22 24 4
gpt4 key购买 nike

我创建了一个 mapkit 应用程序,在其中显示我的位置并每次更新此位置并将此位置设置在 View 中心。我更新我的位置并使用 bool 变量设置在中心。我想在我的 map 上检测滚动或缩放触摸手指,当我这样做时,我的 bool 变量发生了变化,并且没有将我的位置设置在中心,但我不知道如何处理 mapkit 上的触摸事件。

请指导我并告诉我如何在触摸和滚动 map 时更改我的变量。谢谢。这是我的代码:

@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");}
}

最佳答案

使用:

mapView:regionWillChangeAnimated:

它告诉委托(delegate)人 map View 显示的区域即将发生变化。

This method is called whenever the currently displayed map region changes. During scrolling, this method may be called many times to report updates to the map position. Therefore, your implementation of this method should be as lightweight as possible to avoid affecting scrolling performance.

基于 MKMapViewDelegate Protocol Reference

关于ios - 如何通过触摸检测滚动或缩放 mapkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23732309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com