2014-12-26 44 views
0

我只是在玩CLLocation類。我成功完成了下面的代碼。當我按下一個按鈕時,它顯示在ALert信息中的位置。當我再次點擊相同的按鈕時,位置顯示錯誤。另外我還注意到eveytime我必須去設置 - 隱私 - 定位服務,以便每次都可以訪問。當應用程序正確顯示當前位置時,iPhone設置中的位置訪問將變爲空白。再次我必須將其設置爲始終運行該應用程序。我沒有在.plists中添加任何內容。我寫了下面的代碼。我的問題是,它正顯示出正確的位置,只在第一次位置accress設置爲始終位置顯示一次

-(CLLocationCoordinate2D) getLocation{ 
     CLLocationCoordinate2D coordinate; 


     if ([CLLocationManager locationServicesEnabled]) 
     { 


     CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
     locationManager.delegate = self; 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     locationManager.distanceFilter = kCLDistanceFilterNone; 
      [locationManager requestWhenInUseAuthorization]; 
     [locationManager startUpdatingLocation]; 



     CLLocation *location = [locationManager location]; 
     coordinate = [location coordinate]; 
     } 
      return coordinate; 

    } 


    - (void)startTimer { 

     // if ([Run isEqualToString:@"can"]) { 

      i++; 

      NSLog(@"the i value %d", i); 

      Run [email protected]"cant"; 
     CLLocationCoordinate2D coordinate = [self getLocation]; 

     CLGeocoder *ceo = [[CLGeocoder alloc]init]; 
     CLLocation *loc = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude]; //insert your coordinates 

     [ceo reverseGeocodeLocation:loc 
        completionHandler:^(NSArray placemarks, NSError error) 
     { 
      CLPlacemark *placemark = [placemarks objectAtIndex:0]; 

      NSDictionary *eventLocation = [NSDictionary dictionaryWithObjectsAndKeys:placemark.name,@"NAME",placemark.subLocality,@"SUBLOCALITY" ,placemark.postalCode,@"POSTALCODE" ,nil]; 

      LocationDetails= [NSMutableDictionary dictionaryWithObjectsAndKeys:eventLocation,@"value", nil]; 


      NSString *name=placemark.name; 
      NSString *subLocality=placemark.subLocality; 
      NSString *postalCode=placemark.postalCode; 



      NSMutableArray *listData; 
      if (!listData) listData = [[NSMutableArray alloc] init]; 
      [listData addObject:LocationDetails]; 

      NSLog(@"the Location details %@", listData); 

      NSString *location=[NSString stringWithFormat:@"You are now in %@ inside the sublocality of %@ and pin code is %@",name,subLocality,postalCode]; 


      UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Location Update" message:location delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 

      [Alert show]; 


     } 

     ]; 

     // } 

     // contains the code you posted to start the timer 
    } 






    - (IBAction)getLocation:(id)sender { 

     [ self startTimer]; 

    } 

回答

1

而不是使用您的getLocation方法之後,把你所有的CLLocationManager初始化類時初始化。在您的@interface設置屬性來保存用戶的當前位置:

@property (nonatomic, strong) CLLocation *currentLocation; 

並實現委託方法-locationManager:didUpdateLocations:像這樣:

- (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations 
{ 
    // set the current location. The most recent location is always at the end of the 
    // if there are multiple locations 
    _currentLocation = locations[locations.count - 1]; 
} 

然後,而不是CLLocationCoordinate2D coordinate = [self getLocation];,獲得與位置:

CLLocationCoordinate2D coordinate = _currentLocation.coordinate; // I think that's the right property. 

注意:請確保您的課程符合CLLocationManagerDelegate