【问题标题】:How to make forced update of user location?如何强制更新用户位置?
【发布时间】:2011-10-31 02:02:31
【问题描述】:

我需要获取当前用户位置,但我不知道它是否已更改。我可以从 CLLocationManager 请求强制位置更新吗?或者有没有其他方法可以做到这一点?

【问题讨论】:

    标签: iphone ios cocoa-touch mapkit core-location


    【解决方案1】:

    停止并重新启动 LocationManager 应该会强制设备重新获取初始位置。

    [locationManager stopUpdatingLocation];
    [locationmanager startUpdatingLocation];
    

    【讨论】:

      【解决方案2】:

      我在我的一个应用程序中遇到了同样的问题。我实际上不得不更改应用程序结构。 这就是我所做的: 这个类有一个公共方法。 -(无效)定位我;一个抽象类需要实例化这个类并运行 locateMe,然后当 userIsLocated 时会广播一个通知。而另一种方法可以从 (CLLocation *)currentLocation 获取结果坐标;

      #import <Foundation/Foundation.h>
      #import <MapKit/MapKit.h>
      
      @interface ManageUserLocation : NSOperation <CLLocationManagerDelegate> {
          CLLocationManager       *locationManager;
          CLLocation              *currentLocation;
      }
      @property (nonatomic, retain) CLLocationManager     *locationManager;
      @property (nonatomic, retain) CLLocation            *currentLocation;
      
      -(void) locateMe;
      @end
      

      在.m中

          #import "ManageUserLocation.h"
      
          @implementation ManageUserLocation
      
          @synthesize locationManager;
          @synthesize currentLocation; // Other classes use this to get the coordination even better you can make another method that even dont get the direct access to currentLocation. It is up to you. 
      
          - (id)init
          {
              self = [super init];
              if (self) {
               //[self locateMe]; // Just a hook if you need to run it 
              }
              return self;
          }
      
          -(void) locateMe {
              self.locationManager = nil;
              self.locationManager = [[CLLocationManager alloc] init];
              self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
              self.locationManager.delegate = self;
              [self.locationManager startUpdatingLocation];
          }
      
          - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
              // User location has been found/updated, load map data now.
              [self.locationManager stopUpdatingLocation];
              currentLocation = [newLocation copy];   
              // WooHoo Tell everyone that you found the userLocation
              [[NSNotificationCenter defaultCenter] postNotificationName:@"userLocationIsFound" object:nil];
          }
      
          - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
              // Failed to find the user's location. This error occurs when the user declines the location request or has location servives turned off.
              NSString * errorString = @"Unable to determine your current location.";
              UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error Locating" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
              [errorAlert show];
              [errorAlert release];
              [locationManager stopUpdatingLocation];
          }
          - (void)dealloc { #warning dont forget this :) }
      @end
      

      希望对您有所帮助。

      【讨论】:

        【解决方案3】:

        我认为强制更新是违反 Apple 指南的。 苹果表示地理更新将自动发生,但时间未知。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-09-15
          • 1970-01-01
          • 2017-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多