如何在最佳GPS位置调用URL API?

3
我尝试在最佳GPS位置调用URL API。用户点击按钮,触发findMe方法来设置所需精度和距离过滤器,并运行[self.locationManager startUpdatingLocation]
我检查最佳垂直和水平精度,如果它们在下一次迭代中没有更改,我会运行API并[self.locationManager stopUpdatingLocation]
关键是,在此之后,最佳精度会发生变化,并且-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation会被运行几次,即使我停止更新位置。
-(void)findMe {
  self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  self.locationManager.distanceFilter = kCLDistanceFilterNone;
  [self.locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
  double longitude = newLocation.coordinate.longitude;
  double latitude = newLocation.coordinate.latitude;

  if ( ((_bestVertivalAccuracy == 0) || (_bestVertivalAccuracy > newLocation.verticalAccuracy))
    || ((_bestHorizontalAccuracy == 0) || (_bestHorizontalAccuracy > newLocation.horizontalAccuracy)) ) {
      NSLog(@"bestVer: %f", _bestVertivalAccuracy);
      NSLog(@"bestHor: %f", _bestHorizontalAccuracy);
      NSLog(@"Given ver: %f", newLocation.verticalAccuracy);
      NSLog(@"Given hor: %f", newLocation.horizontalAccuracy);
      NSLog(@"-------------------------------------------------------");

      _bestVertivalAccuracy = newLocation.verticalAccuracy;
      _bestHorizontalAccuracy = newLocation.horizontalAccuracy;
      return;
  }

  [_locationManager stopUpdatingLocation];

  [POSharedLocation sharedInstance].lng = longitude;
  [POSharedLocation sharedInstance].lat = latitude;
  [POSharedLocation sharedInstance].showUserLocation = YES;

  NSLog(@"Longitude : %f", longitude);
  NSLog(@"Latitude : %f", latitude);
  NSLog(@"Acc. Ver: %f", newLocation.verticalAccuracy);
  NSLog(@"Acc. Hor: %f", newLocation.horizontalAccuracy);
  NSLog(@"-------------------------------------------------------");

  _resultsVC = [[ResultsListViewController alloc] init];
  UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:_resultsVC];

  NSString *locationQuery = [NSString stringWithFormat:@"/json?method=find&lat=%f&lng=%f&distance=%f", latitude, longitude, _distance];

  NSString *url = [API_URL stringByAppendingString:locationQuery];
  NSString* urlEncoded = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


  // For debuging only
  NSLog(@"URL: %@", urlEncoded);

  NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlEncoded]];

  // Run an asynchronous connection and download the JSON
  (void)[[NSURLConnection alloc] initWithRequest:request delegate:_resultsVC startImmediately:YES];
  [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

  [self presentModalViewController:navVC animated:YES];
  [navVC release];
}

输出结果:

2013-05-30 12:59:13.906 MyApp[30569:907] bestVer: 0.000000
2013-05-30 12:59:13.918 MyApp[30569:907] bestHor: 0.000000
2013-05-30 12:59:13.922 MyApp[30569:907] Given ver: 3.000000
2013-05-30 12:59:13.927 MyApp[30569:907] Given hor: 50.000000
2013-05-30 12:59:13.933 MyApp[30569:907] -------------------------------------------------------
2013-05-30 12:59:14.112 MyApp[30569:907] Longitude : 16.934037
2013-05-30 12:59:14.114 MyApp[30569:907] Latitude : 51.096827
2013-05-30 12:59:14.116 MyApp[30569:907] Acc. Ver: 10.000000
2013-05-30 12:59:14.119 MyApp[30569:907] Acc. Hor: 1414.000000
2013-05-30 12:59:14.131 MyApp[30569:907] -------------------------------------------------------
2013-05-30 12:59:14.138 MyApp[30569:907] URL: http://example.com/json?method=find&lat=51.096827&lng=16.934037&distance=1.000000
2013-05-30 12:59:14.241 MyApp[30569:907] Longitude : 16.933006
2013-05-30 12:59:14.244 MyApp[30569:907] Latitude : 51.096140
2013-05-30 12:59:14.247 MyApp[30569:907] Acc. Ver: 10.000000
2013-05-30 12:59:14.248 MyApp[30569:907] Acc. Hor: 1414.000000
2013-05-30 12:59:14.250 MyApp[30569:907] -------------------------------------------------------
2013-05-30 12:59:14.253 MyApp[30569:907] URL: http://example.com/json?method=find&lat=51.096140&lng=16.933006&distance=1.000000
2013-05-30 12:59:14.256 MyApp[30569:907] Warning: Attempt to present <UINavigationController: 0x1e068fd0> on <HomeViewController: 0x1d572130> while a presentation is in progress!
2013-05-30 12:59:14.260 MyApp[30569:907] Longitude : 16.932491
2013-05-30 12:59:14.262 MyApp[30569:907] Latitude : 51.095797
2013-05-30 12:59:14.263 MyApp[30569:907] Acc. Ver: 10.000000
2013-05-30 12:59:14.264 MyApp[30569:907] Acc. Hor: 1414.000000
2013-05-30 12:59:14.269 MyApp[30569:907] -------------------------------------------------------
2013-05-30 12:59:14.287 MyApp[30569:907] URL: http://example.com/json?method=find&lat=51.095797&lng=16.932491&distance=1.000000
2013-05-30 12:59:14.291 MyApp[30569:907] Warning: Attempt to present <UINavigationController: 0x1d599790> on <HomeViewController: 0x1d572130> while a presentation is in progress!
2013-05-30 12:59:14.298 MyApp[30569:907] Longitude : 16.932234
2013-05-30 12:59:14.301 MyApp[30569:907] Latitude : 51.095625
2013-05-30 12:59:14.302 MyApp[30569:907] Acc. Ver: 10.000000
2013-05-30 12:59:14.303 MyApp[30569:907] Acc. Hor: 1414.000000
2013-05-30 12:59:14.304 MyApp[30569:907] -------------------------------------------------------
2013-05-30 12:59:14.307 MyApp[30569:907] URL: http://example.com/json?method=find&lat=51.095625&lng=16.932234&distance=1.000000
2013-05-30 12:59:14.309 MyApp[30569:907] Warning: Attempt to present <UINavigationController: 0x1e06c930> on <HomeViewController: 0x1d572130> while a presentation is in progress!

从图中可以看出,API被调用了几次,但实际上只需要调用一次。有没有什么好的方法可以改进呢?


1
有两种方法可以实现这个功能,1)使用一个flag, 在请求URL之前检查flag=false。当URL请求被发送后,将flag=true。2)在视图控制器初始化期间,开始获取位置更新,存储坐标,并在findMe方法中只需使用已存储的坐标进行URL请求即可。 - Amar
@Amar,你可以将这个评论添加为答案。 - Ishu
好的,但为什么位置精度比以前更不准确了? - Stak
委托的第一个回调通常包含缓存位置,可能是高精度(50米)的缓存位置。您应该检查位置的年龄并丢弃旧位置(> 30秒),如果您不想使用缓存位置,请参见此处的示例 - progrmr
你正在保存最佳准确度,但没有保存与最佳准确度一起接收到的最佳位置。不要期望准确度连续3次保持在最佳值,它可能处于边界上,并在两个值之间来回切换。当你获得所需的准确度时,请使用它并停止。 - progrmr
@progrmr 但我保存最佳准确度并检查位置的准确度。当准确度与先前更新位置的准确度相同时,我使用该位置并停止更新位置。 - Stak
3个回答

3
尝试将您的locationManagers委托设置为nil。
_locationManager.delegate = nil;

1
不要依赖位置管理器调用您的委托方法特定次数。您无法控制位置管理器,因此依赖超出文档范围的特定行为始终会给您带来问题。但是,您可以控制自己的代码,使其按照您的意愿进行操作:如果您不再需要位置更新,请在不需要时忽略委托方法的更新,或将位置管理器的委托设置为nil。

0

根据建议,将其作为答案添加。

两种方法来做到这一点:

  1. 使用一个标志,在发送URL请求之前检查 flag=false。在发送URL请求后设置 flag=true
  2. 在视图控制器初始化期间,开始获取位置更新,存储坐标,并在 findMe 方法中使用存储的坐标进行URL请求。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接