3

我已经在我的应用程序中实现了核心位置。

当用户允许在应用程序启动时共享位置时,一切正常。
但是当用户不分享他的位置时,“南大西洋”会显示在应用程序的位置字段中。

应该做些什么来避免这种情况以及当用户不共享他的位置时应该显示什么。

我正在使用此代码获取位置:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    [self.window makeKeyAndVisible];


    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

    CLGeocoder *ceo = [[CLGeocoder alloc]init];
    CLLocation *loc = [[CLLocation alloc]initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude]; //insert your coordinates

    [ceo reverseGeocodeLocation: loc completionHandler:
     ^(NSArray *placemarks, NSError *error)
     {
         CLPlacemark *placemark = [placemarks objectAtIndex:0];
         NSLog(@"placemark %@",placemark);
         //String to hold address
         locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
         NSLog(@"addressDictionary %@", placemark.addressDictionary);

         NSLog(@"placemark %@",placemark.region);
         NSLog(@"placemark %@",placemark.country);  // Give Country Name
         NSLog(@"placemark %@",placemark.locality); // Extract the city name
         NSLog(@"location %@",placemark.name);
         NSLog(@"location %@",placemark.ocean);
         NSLog(@"location %@",placemark.postalCode);
         NSLog(@"location %@",placemark.subLocality);

         NSLog(@"location %@",placemark.location);
         //Print the location to console
         NSLog(@"I am currently at %@",locatedAt);
     }];


    return YES;
}



- (NSString *)deviceLocation
{
    NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
    return theLocation;

}
4

2 回答 2

4

您可以检查[CLLocationManager authorizationStatus]以验证用户是否允许使用类似的位置服务

int result = [CLLocationManager authorizationStatus];

它有返回值

typedef enum {
    kCLAuthorizationStatusNotDetermined = 0,
    kCLAuthorizationStatusRestricted,
    kCLAuthorizationStatusDenied,
    kCLAuthorizationStatusAuthorized
} CLAuthorizationStatus;
于 2012-11-20T07:11:55.083 回答
2

有关如何获取用户位置的信息,请参阅位置感知编程指南。最重要的是,您必须创建CLLocationManager位置管理器(就像您一样),然后启动位置服务(低功耗显着变化位置服务或标准位置服务)并等待该CLLocationManagerDelegate方法didUpdateLocations返回位置(或用于didFailWithError报告错误,或者是因为应用授权失败,或者因为设备由于某种原因无法满足位置请求)。但是,您不能只从CLLocationManager. didUpdateLocations在可靠地开始使用经度和纬度之前,您必须等待服务调用。

位置感知编程指南将引导您完成所有这些,包括一些非常有用的代码示例。

于 2012-11-20T07:12:18.197 回答