1
我無法理解CLLocationManager如何獲取我當前的位置座標?如果我不調用[locationManager startUpdatingLocation]並且不在locationManager.location.coordinate中設置參數。locationManager如何獲取座標?
ViewController.h
@interface ViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
@property (weak, nonatomic) IBOutlet MKMapView* mapView;
@end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
[self.mapView setShowsUserLocation:YES];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
-(void) mapView:(MKMapView *) mv didAddAnnotationViews:(NSArray *)views {
MKCoordinateRegion region;
region = MKCoordinateRegionMakeWithDistance(locationManager.location.coordinate, 1000, 1000);
[mv setRegion:region animated:YES];
}
目前尚不清楚你的問題是什麼。我建議你編輯它以包含一個明確表示的問題,以便產生一個簡明的答案。 –
它可能是先前執行的緩存值。查看locationManager.location.timestamp。它甚至可能是由將地圖視圖的showsUserLocation設置爲YES引起的。另外,如果您使用地圖視圖來顯示用戶位置,則不需要CLLocationManager。 – Anna