Chandan Reddy Chandan Reddy - 1 month ago 10
Objective-C Question

Stop centralizing the user location when zoom out/in or move to other location in ios

Trying to track user location using MKMapView.

Using below code

-(void)viewDidLoad{

myMapView.delegate = self;

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
arrDate = [[NSMutableArray alloc]init];


#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {

[self.locationManager requestWhenInUseAuthorization];

}
#endif

myMapView.showsUserLocation = YES;
[myMapView setMapType:MKMapTypeStandard];
[myMapView setZoomEnabled:YES];
[myMapView setScrollEnabled:YES];

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//[self.locationManager startUpdatingLocation];


//View Area

MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[myMapView setRegion:region animated:YES];

}

-(IBAction)start{
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}

self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeFitness;

// Movement threshold for new events.
self.locationManager.distanceFilter = 8; // meters

[self.locationManager startUpdatingLocation];

}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locationss
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(myMapView.userLocation.location.coordinate, 900, 900);
[myMapView setRegion:[myMapView regionThatFits:region] animated:YES];
}


Problem

when i want zoom out/in or moving map to other location while updating the location. it is not working and moving back to present user location.

how can i solve this problem ?

Answer

In your @interface add a control flag:

@property (nonatomic, readwrite) BOOL userScrolling;

In your @implementation add/override these bits of code:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Other set-up code here...

    // Used for detecting manual panning/zooming of the map
    UIPanGestureRecognizer* panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didManipulateMap:)];
    UIPinchGestureRecognizer* pinchRec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(didManipulateMap:)];

    [panRec setDelegate:self];
    [pinchRec setDelegate:self];

    [myMapView addGestureRecognizer:panRec];
    [myMapView addGestureRecognizer:pinchRec];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

- (void)didManipulateMap:(UIGestureRecognizer*)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
        _userScrolling = YES;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locationss {
    if (!_userScrolling) {
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(myMapView.userLocation.location.coordinate, 900, 900);
        [myMapView setRegion:[myMapView regionThatFits:region] animated:YES];
    }
}

- (IBAction)locateMeButtonPressed:(id)button {
    _userScrolling = NO;
    [self.locationManager startUpdatingLocation];
}

That should do it.

Comments