0

我无法获取当前位置。当我在不同的地方启动我的应用程序时,应用程序可以获得最后一个位置。但我不想最后一个位置。如果您关闭应用程序并重新启动它,现在应用程序可以获取当前位置。即使是首次启动应用程序,我如何才能获得当前位置?

- (void)viewDidLoad
{
    [super viewDidLoad];

    [locationManager startUpdatingLocation];

    self.mapView.delegate = self;
    [self.mapView setShowsUserLocation:YES];

    locationManager.delegate=self;

    locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=kCLDistanceFilterNone;

    location = [locationManager location];

    CLLocationCoordinate2D coord;
    coord.longitude = location.coordinate.longitude;
    coord.latitude = location.coordinate.latitude;
    lat = coord.latitude;
    longt = coord.longitude;
}
4

4 回答 4

8

[locationManager startUpdatingLocation];您在设置其委托之前正在做

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;

[locationManager startUpdatingLocation];

并实现其委托方法

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{

}
于 2013-03-07T07:49:43.940 回答
2

要获取当前位置及其坐标,您只需在您的viewDidLoad方法中执行此操作:

- (void)viewDidLoad {
  [super viewDidLoad];
  locationManager = [[CLLocationManager alloc] init];
  locationManager.delegate = self;
  locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
  locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
  [locationManager startUpdatingLocation];
}

关于更新位置,请使用此方法:

    - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation{
// your code here...
}
于 2013-03-07T08:08:12.463 回答
0

You should read the documentation provided in the Location Awareness Programming Guide.

Specifically, when you ask for the current location, the system returns the last known location right away so you can do something useful with it. If you don't care about past locations, you can discard it and only use more recent location information by looking at the timestamp property of the CLLocation returned to determine how recent it is.

于 2013-03-07T07:56:27.227 回答
0

You should really read the CLLocationManager documentation.

Wat you are doing will not work, since it will take some time determine the device location. Therefor you will need to wait until the CLLocationManager notifies you that a location has been determent.

You will need to implement the CLLocationManagerDelegate which will tell you if a location is determent or if the location determination failed.

Also you should also check if location can be determent with:

if ([CCLocationManager locationServicesEnabled]) {
    // The location services are available.
}

You should also check wether you are authorize to use the location services with [CCLocationManager authorizationStatus].

于 2013-03-07T07:56:45.917 回答