簡體   English   中英

如何跟蹤用戶的位置剛好足以抓住位置?

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

我有三點,但實際上只有第一點才是最重要的

  • 為了延長電池壽命,我試圖將locationManager的開啟時間足夠長,以獲取用戶的位置,然后將其關閉。 這樣做的最佳方法是什么?

  • 我並不總是需要知道用戶的位置,但是當用戶點擊搜索時具有相對准確的位置很重要(可能在十幾個城市街區,10,000米之內,甚至可能不那么准確)。

  • 我正在向服務器發送請求,然后獲取結果,在延遲之后,我需要用戶位置的更准確版本(100米以內)。

我不知道其中有多少太挑剔了,但是如果后兩點在電池方面可能/有效的話,請讓我知道該怎么做!

CLLocationManager的代表收到符合您要求的准確性的位置更新時,您可以輕松地停止跟蹤位置。

要開始更新位置,請執行以下操作:

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

然后實現適當的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
  }
}

通過檢查返回的返回位置的准確性,並停止更新或讓它們繼續(如果需要更高的准確性),您應該能夠調整以滿足您的要求。

最好在開始更新位置時設置一個計時器,如果在設定的時間內沒有找到位置,則停止更新。 您還應該實現locationManager:didFailWithError:委托方法,以檢查是否可以訪問定位服務。

檢索到用戶的位置信息后,可以停止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