【问题标题】:Stop centralizing the user location when zoom out/in or move to other location in ios缩小/缩小或移动到 ios 中的其他位置时停止集中用户位置
【发布时间】:2017-02-13 13:09:41
【问题描述】:

尝试使用 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];
}

问题

当我想在更新位置时缩小/缩小或将地图移动到其他位置。它不工作并移回当前用户位置。

我该如何解决这个问题?

【问题讨论】:

    标签: ios objective-c mkmapview mkuserlocation


    【解决方案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];
    }
    

    应该可以的。

    【讨论】:

      猜你喜欢
      • 2020-12-24
      • 2023-03-29
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      相关资源
      最近更新 更多