2012-12-12 52 views
3

我正在研究侧重于建筑物(校园)中的行人的iOS应用程序。我已经开发了一个足够好的(我相信)用户的位置更新,但是,我希望任何人比我更专家给我一些提示,建议准确的位置,电池的问题等。此外,是否有可能停止位置更新和n秒后再次启动?是我为了节约能源而做的一个想法。目前,应用程序正在检测当前位置,但蓝点(用户)仍在移动,我不喜欢这样。有什么可以做的?用户的位置和电池消耗

下面是我的didUpdateToLocation方法:从一个文件中(存储设备)

应用正在读建筑的信息

- (void)viewDidLoad{ 

     [super viewDidLoad]; 

     if ([self checkForInternet]) { 

      _locationManager = [[CLLocationManager alloc] init]; 
      _locationManager.delegate = self; 
      _locationManager.distanceFilter = 10.0f; 
      _locationManager.desiredAccuracy = 20.0f; 

      [_locationManager startUpdatingLocation]; 

     } 
    } 

    - (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation { 

     if (newLocation.horizontalAccuracy > manager.desiredAccuracy) return; 

     [self.mapView removeAnnotations:listOfAnn]; 
     [listOfAnn removeAllObjects]; 
     if ([manager locationServicesEnabled]) { 

      for (NSString* row in rows){ 
       NSArray* cells = [row componentsSeparatedByString:@"\t"]; 

       CLLocationCoordinate2D newCoord; 
       newCoord.latitude = [[cells objectAtIndex:5]floatValue]; 
       newCoord.longitude = [[cells objectAtIndex:6]floatValue]; 

       CLLocation *locB = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude]; 
       CLLocation *centerLoc = [[CLLocation alloc] initWithLatitude:CAMPUS_LATITUDE longitude:CAMPUS_LONGITUDE]; 
       CLLocationDistance borders = [locB distanceFromLocation:centerLoc]; 

       if ((int)round(borders) > 500) { 

        BuildingViewController *newBuilding = [[BuildingViewController alloc] initBuildingWithName:[cells objectAtIndex:2]                          coordinates:newCoord shortDescription:[cells objectAtIndex:4] image:(NSString*)[cells objectAtIndex:3]                            inDistance: borders]; 

        if ([[prefs stringForKey:newBuilding.title] isEqualToString:[prefs stringForKey:@"userName"]]) { 
         [newBuilding retrieveID]; 
         [listOfAnn addObject:newBuilding]; 
        } 

       } else{ 

        CLLocation *locA = [[CLLocation alloc] initWithLatitude:newCoord.latitude longitude:newCoord.longitude]; 

        CLLocationDistance distance = [locA distanceFromLocation:locB]; 

        BuildingViewController *newBuilding = [[BuildingViewController alloc] initBuildingWithName:[cells objectAtIndex:2] 
                 coordinates:newCoord shortDescription:[cells objectAtIndex:4] image:(NSString*)[cells objectAtIndex:3] 
                 inDistance: distance]; 

        if ((int)round(distance) < 100 || 
         [[prefs stringForKey:newBuilding.title] isEqualToString:[prefs stringForKey:@"userName"]]){ 
          [newBuilding retrieveID]; 
          [listOfAnn addObject:newBuilding]; 
        } 

       } 

      } 
      [self.mapView addAnnotations:listOfAnn]; 
     } 
} 

回答

3

ΓειασουΠαναγιώτη。

You should read the official documentation about location services

这是一个很好的指南,应该得到面面俱到。 我会为你做一个快速回顾并向你解释每种可用方法的优点和缺点,因为我已经为我们的应用程序广泛使用了Core Location服务:

有三种不同的方式可以在iOS中获取用户位置。

  1. 普通GPS定位monitorins(优点:非常acurate的且更新快,缺点:消耗电池电量太快)
  2. 显着位置服务(优点:非常的电池效率,可以开始从后台应用程序,甚至当如果输入区域,则不需要背景位置监控任务和权限,缺点:在确定位置和接收位置变化的更新时非常不准确)
  3. 区域监控(优点:非常节电,可以启动应用程序背景,即使在输入区域时终止,也不需要背景位置监视任务和权限缺点:在gett中不准确关于输入区域的通知,最多20个区域可以通过应用程序进行监控)

因此,根据所需的准确性,您必须选择服务。 当然,如果您要采用GPS方式,就像您在代码中使用的一样,您应该在更新后关闭位置更新,并且仅在一段时间后再次请求位置,否则您的应用程序将耗尽用户的电池。

当然,您可以随时重新启动用户的位置更新。 如果您的应用程序中的许多视图控制器需要位置更新,我建议您为位置管理器创建一个单例类!

至于GPS监控,在你CoreLocation委托方法,didUpdateToLocation,请执行下列操作:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 

    NSDate* eventDate = newLocation.timestamp; 
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
    //prevent Location caching... (only accepts cached data 1 minute old) 
    if(abs(howRecent) < 60.0) { 
     [self.locationManager stopUpdatingLocation]; 
     //capture the location... (this is your code) 

     //update location after 90 seconds again 
     [self performSelector:@selector(startUpdatingLocation) withObject:nil afterDelay:90]; 
    } 

} 

,并再次更新位置:

- (void)startUpdatingLocation { 
    if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorized || [CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined) { 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     [locationManager startUpdatingLocation]; 
    } 
} 

,并确保我们已经取消了如果用户更改viewController,执行选择器,将其添加到:

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated];  
    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
} 
+0

ΓειάσουΛευτέ ρη, 谢谢你对这篇文章的快速回复。我已经阅读过官方文件,但是再次感谢你提及这一点。我正在使用GPS监控,因为正如我所说的,我想为用户提供一个准确的位置。如果重要的位置服务能够满足我的需求,我不能100%确定。你知道重要位置服务的准确度吗? – OutOfBoundsException

+0

Panayioti,正如我所提到的,重要的定位服务和区域监控都非常不准确,并且依靠GSM天线来找到您的位置,而不是GPS。如果您需要准确性,那么请使用GPS方法,但正如我所说的,一旦您获得更新,停止GPS并在给定时间后重新启动它(取决于您的需要,这可能是几秒钟,几分钟等)。 我正在添加一些额外的代码,以便在原始答案中使用gps更新。请看看 – Lefteris

+0

谢谢。你知道为什么蓝点仍在移动,即使我打电话[经理stopUpdatingLocation]; ? – OutOfBoundsException