缩小/缩小或移动到ios中的其他位置时停止集中用户位置

时间:2016-10-05 07:37:47

标签: ios objective-c mkmapview mkuserlocation

尝试使用MKMapView跟踪用户位置。

使用以下代码

-(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];
}

问题

当我想缩小/放大或在更新位置时将地图移动到其他位置时。它没有工作并且回到现在的用户位置。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

@interface添加控制标记:

@property (nonatomic, readwrite) BOOL userScrolling;

@implementation添加/覆盖这些代码:

- (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];
}

应该这样做。