应用程序的地图屏幕显示用户的当前位置。我希望允许用户“浏览”地图(滚动并浏览其他区域),我有一个按钮,用户将当前位置返回到地图上的点但是我是发生的事情是该应用程序不允许用户“浏览”地图并保留他们正在查看的视图,而是直接跳回用户的当前位置。
以下是一些代码:
-(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];
}
为什么地图不断跳回到用户当前位置的任何想法?
非常感谢!
答案 0 :(得分:1)
因为,在你的 - (void)locationManager:didUpdateToLocation:fromLocation:方法
你每次都将mapview的区域设置为更新的位置。
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];
}