简体   繁体   English

如何跟踪用户的位置刚好足以抓住位置?

[英]How do I track user's location just long enough to grab the location?

I have three points, but really only the first one is the most important 我有三点,但实际上只有第一点才是最重要的

  • In order to preserve battery life, I'm trying to have the locationManager turn on just long enough to get the user's location and then shut off. 为了延长电池寿命,我试图将locationManager的开启时间足够长,以获取用户的位置,然后将其关闭。 What is the best means to do so? 这样做的最佳方法是什么?

  • I don't always need to know the user's location, but having a relatively accurate location when the user hits search is important (within perhaps a couple dozen city blocks, 10,000 meters, maybe even less accurate). 我并不总是需要知道用户的位置,但是当用户点击搜索时具有相对准确的位置很重要(可能在十几个城市街区,10,000米之内,甚至可能不那么准确)。

  • I'm sending a request to a server and then getting the results, and after THAT delay, I need a more accurate version of the user location (within 100 meters is fine). 我正在向服务器发送请求,然后获取结果,在延迟之后,我需要用户位置的更准确版本(100米以内)。

I don't know how much of this is too nitpicky, but if the last two points are possible/efficient battery-wise, then please let me know how to do it! 我不知道其中有多少太挑剔了,但是如果后两点在电池方面可能/有效的话,请让我知道该怎么做!

You can easily stop tracking location when your CLLocationManager 's delegate receives a location update meeting your desired accuracy. CLLocationManager的代表收到符合您要求的准确性的位置更新时,您可以轻松地停止跟踪位置。

To start updating location, do something like: 要开始更新位置,请执行以下操作:

CLLocationManager *locationManager = [CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];

Then implement the appropriate CLLocationManagerDelegate method: 然后实现适当的CLLocationManagerDelegate方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
  CLLocation *foundLocation = [locations lastObject];
  if (foundLocation && foundLocation.horizontalAccuracy < kYourDesiredAccuracyInMetres)
  {
    [manager stopUpdatingLocation];
    //Do whatever else with the location you've established
  }
}

You should be able to tweak this meet your requirements, by checking the accuracy of the returned locations returned and either stopping the updates or letting them continue (if you need better accuracy). 通过检查返回的返回位置的准确性,并停止更新或让它们继续(如果需要更高的准确性),您应该能够调整以满足您的要求。

It's also a good idea to set a timer when you start updating location, and stop updates if you haven't found a location within a set amount of time. 最好在开始更新位置时设置一个计时器,如果在设定的时间内没有找到位置,则停止更新。 You should also implement the locationManager:didFailWithError: delegate method to check whether you can access location services at all. 您还应该实现locationManager:didFailWithError:委托方法,以检查是否可以访问定位服务。

LocationManager could be stopped after location information of user has been retrieved . 检索到用户的位置信息后,可以停止LocationManager。

CLLocationManager* locationManager = [ [ CLLocationManager alloc] init];

locationManager.delegate = self; //we must implement the protocol

locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.headingFilter = kCLHeadingFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers; 


//To turn on gps (if it isn't on already)
[locationManager startUpdatingLocation];

//To turn gps off (if no other apps are listening)
[locationManager stopUpdatingLocation];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM