簡體   English   中英

在ios中縮小/放大或移動到其他位置時停止集中用戶位置

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

嘗試使用 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];
}

問題

當我想在更新位置的同時縮小/放大或將地圖移動到其他位置時。 它不工作並移回當前用戶位置。

我怎么解決這個問題 ?

在您的@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];
}

那應該這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM