0

应用程序的地图屏幕显示用户的当前位置。我想允许用户“浏览”地图(滚动并探索其他区域),我有一个按钮,可以将用户返回到地图上的点及其当前位置,我正在发生的事情是应用程序是'不允许用户'浏览'地图并保留他们正在查看的视图,而是直接跳回用户的当前位置。

这是一些代码:

-(void) setupLocation {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        //    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
        [locationManager startUpdatingLocation];
    }

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        NSLog(@"didFailWithError: %@", error);
    }

        - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        //    [self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate animated:YES];

        tempLat = newLocation.coordinate.latitude;
        tempLon = newLocation.coordinate.longitude;
        CLLocationCoordinate2D currentLocation = CLLocationCoordinate2DMake(tempLat, tempLon);
        MKCoordinateRegion viewRegionLocation = MKCoordinateRegionMakeWithDistance(currentLocation, 100, 100);
        [self.mapView setRegion:viewRegionLocation animated:YES];

        locationNew = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
        locationOld = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];

    }

我也有:

- (IBAction)stopUpdating:(id)sender {
    [locationManager stopUpdatingLocation];
}

和:

- (IBAction)findMe:(id)sender {
    [locationManager startUpdatingLocation];
}

关于为什么地图不断跳回用户当前位置的任何想法?

非常感谢!

4

1 回答 1

1

因为,在您的- (void)locationManager: didUpdateToLocation: fromLocation: 方法中

中,您每次都将地图视图的区域设置为更新的位置。

删除这部分将解决您的问题。

  MKCoordinateRegion viewRegionLocation = MKCoordinateRegionMakeWithDistance(currentLocation, 100, 100);
  [self.mapView setRegion:viewRegionLocation animated:YES];

编辑1:

在您的 viewDidLoad 方法中添加这一行,

self.mapView.showsUserLocation = YES;

像这样更改“查找我”按钮按下方法,

-(IBAction) findMeButtonPressed:(id)sender;{
    MKCoordinateRegion viewRegionLocation = MKCoordinateRegionMakeWithDistance([self.locationManager location].coordinate, 100, 100);
    [self.mapView setRegion:viewRegionLocation animated:YES];
}
于 2013-05-12T07:08:00.287 回答