用户位置和电池消耗 [英] User's location and battery's consumption

查看:69
本文介绍了用户位置和电池消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an iOS app that's focuses on pedestrians in a block of buildings (campus). I have developed a good enough (I believe) user's location update but, I would like anyone more expert than me to give me some tips, advices on accurate location, battery's issues etc. In addition, is it possible to stop the location update and start it again after n seconds? Is a thought that I did in order to save energy. As it is at the moment, app is detecting current location, but, blue dot (user) is still moving around and I don't like that. There is something that can be done?

下面是我的didUpdateToLocation方法:

Below is my didUpdateToLocation method:

应用正在从文件(存储在设备上)读取建筑物的信息

    - (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];
      }
 }

推荐答案

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

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

您应阅读有关的官方文档定位服务

这是一个很好的指南,应该涵盖所有内容. 我将为您做一个简短的回顾,并向您解释每种可用方法的利弊,因为我已经为我们的应用广泛使用了Core Location服务:

It is an excellent guide and should get cover everything. I will do a quick recap for you and explain you the pros and cons for each available method, as I have worked extensively with Core Location services for our App:

有3种不同的方法可以在iOS中获取用户位置.

There are 3 different ways to get the users location in iOS.

  1. 普通的GPS位置监控器(优点:非常准确并能快速更新,缺点:耗尽电池的速度太快了)
  2. 重要的位置服务(优点:电池使用效率高,即使在进入某个区域时终止,也可以从后台启动应用程序,而无需后台位置监视任务和权限),缺点:确定位置和接收时都非常不准确位置更改的更新)
  3. 区域监视(优点:电池效率很高,即使输入了区域也可以终止,即使从后台启动应用程序也可以从后台启动应用程序,而无需后台位置监视任务和权限缺点:获取关于所输入区域的通知不准确,最多20个区域可以通过应用监控)

因此,根据您想要的准确性,您必须选择服务. 当然,如果您使用GPS方式(如在代码中使用的那样),您应该始终在获取更新后关闭位置更新,并仅在经过一定时间后再次请求位置时间,否则您的应用会耗尽用户的电量.

So depending on the accuracy you want, you have to select the service. Of course, if you are going the GPS way, like you use in your code, you should always turn off the location updating after you get an update and request the position again, only after a certain period of time, else your app will drain the user's battery.

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

And of course you can restart the user's location updates at anytime. If you require location updates in many view controllers in your app, I suggest you create a singleton class for the location manager!

对于GPS监视,在您的CoreLocation委托方法didUpdateToLocation中,执行以下操作:

As for GPS monitoring, in your CoreLocation delegate method, didUpdateToLocation, do the following:

- (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的情况下我们已经取消了performSelector,请将其添加到:

and to be sure we have canceled our performSelector in case the user changes viewController, add this to:

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

这篇关于用户位置和电池消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆