如何强制更新用户位置?

时间:2011-08-20 05:12:56

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

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

3 个答案:

答案 0 :(得分:13)

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

[locationManager stopUpdatingLocation];
[locationmanager startUpdatingLocation];

答案 1 :(得分:3)

我在其中一个应用中遇到了同样的问题。我实际上不得不改变应用程序结构。 这就是我所做的: 这个类有一个公共方法。 - (无效)locateMe;抽象类需要实例化此类并运行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
<。> <。>

    #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

希望这有帮助。

答案 2 :(得分:0)

我认为违反Apple指令强制更新。 Apple称地理更新将自动发生,但在未知时间。