2012-08-01 49 views
1

問題是位置管理器未更新位置。 (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLoc fromLocation:(CLLocation *)oldLoc函數被調用,但它將新位置顯示爲相同的舊位置。CLLocationManager未更新位置

以下是我的一段代碼: 在我viewDidLoad方法中,我創建CLLocationManager

-(void) viewDidLoad 
{ 

    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 
    self.locationManager.distanceFilter = kCLDistanceFilterNone; 
    // created a timer to call locationUpdate method 
    [NSTimer scheduledTimerWithTimeInterval:20 target: self selector: @selector(locationUpdate) userInfo: nil repeats: YES];  

} 

-(void)locationUpdate 
{ 

    [self.locationManager startUpdatingLocation]; 

} 

-(void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLoc 
      fromLocation:(CLLocation *)oldLoc 
{ 

    NSLog(@"in locationmanager did update %f",newLoc.coordinate.latitude); 
    MKCoordinateRegion region = 
    MKCoordinateRegionMakeWithDistance(newLoc.coordinate, 0.01,  0.02); 
    [self.mapView setRegion:region animated:YES]; 
    [self.locationManager stopUpdatingLocation]; 

} 

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 

    if ([annotation isKindOfClass:[MKUserLocation class]]) 
    { 
     MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.02); 
     MKCoordinateRegion region = MKCoordinateRegionMake(mapView.userLocation.coordinate, span); 
     [_mapView setRegion:region animated:YES]; 
     [_mapView regionThatFits:region]; 

    } 

我在的NSLog我得到(@「中的LocationManager沒有更新%F」的值的對象,newLoc .coordinate.latitude)始終是相同的 - 雖然我從當前位置開始移動超過2公里。

請幫助我如何獲取確切的新位置,只要有位置更新。提前致謝。

+0

你爲什麼重新啓動使用NSTimer的可憐的位置管理器? **如果你已經閱讀過文檔,你會發現只需調用**'startUpdatingLocation' **就可以了!** – 2012-08-01 06:02:36

+0

在添加一個定時器之前,我試圖在視圖中調用一次[locationManager startUpdatingLocation] ,但在locationUpdate委託方法中,我得到了與舊的一樣的新位置。 – Sonal0706 2012-08-01 06:31:32

+0

這可能是因爲......歐盟......你不動? – 2012-08-01 07:54:47

回答

3

您在

-(void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLoc 
     fromLocation:(CLLocation *)oldLoc 

[self.locationManager stopUpdatingLocation]; 

停止位置管理。這種委託方法在用戶每次移動時都會被調用,並且可以在開始時爲您提供舊的(緩存的)數據,所以當您獲得第一次定位修復時,您可以立即停止它,您可能每次都會獲得一個緩存位置。修復很簡單,只是不要停止位置管理器,而是當你的viewController消失或在類似的有用的地方。

+0

感謝您的回覆。其實我正在測試iPad的應用程序與無線..並且因爲它沒有一個GPS芯片,我沒有得到任何位置更新...代碼在iphone上工作... – Sonal0706 2012-08-08 06:00:37