Search code examples
iosobjective-ctouchmapkituitouch

how to detect scroll or zoom on mapkit with touch


I create one mapkit app that in this show my location and update this location every time and set this location in center of view. I update my location and set in center with Boolean variable. I want detect scroll or zoom touch finger on my map and when I do it my boolean variable changed and don't set my location on center but I don't know about handling touch event on mapkit.

please guide me about that and tell me how to change my variable when touching and scroll map. thanks. this is my code:

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

Solution

  • Use:

    mapView:regionWillChangeAnimated:
    

    which tells the delegate that the region displayed by the map view is about to change.

    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.

    Based on MKMapViewDelegate Protocol Reference