在appDelegate中获取用户位置

时间:2013-07-08 07:32:18

标签: iphone ios cllocationmanager cllocation appdelegate

继承人的情景。我有两个UIViewControllers。可以说,PresentinLondonViewControllerNotPresentinLondonViewController。我想先加载的视图应该取决于当前用户。让我们说当且仅当当前用户位置是伦敦时,我希望PresentViewController显示NotPresentViewController。

我做了很多研究,找不到任何具体的东西,甚至试过我的手上的协议。对我不起作用。

3 个答案:

答案 0 :(得分:1)

您可以使用CLLocationManager获取当前位置,之后您可以reverse geocoding从坐标中获取地址。

在该地址中,如果它准确无误,您会找到符合您需求的“London”字样。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中执行此操作:

CLLocationManager *manager = [[CLLocationManager alloc] init];
manager.delegate = self;
//Here you set the Distance Filter that you need
manager.distanceFilter = kCLDistanceFilterNone;
// Here you set the Accuracy 
  manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[manager startUpdatingLocation];

在委托函数中:

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


        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
            //Picked the first placemark
             CLPlacemark *placemark = placemarks[0];
           //you can play and see how to grab the key name that you want.
             NSLog(@"Address %@",[NSString stringWithFormat:@"%@ %@",placemark.thoroughfare,addressNumber]);
          //Do your additional setup here

        }];
        [geocoder release];
        [location release];

}

如果您想延迟加载以便您可以抽出时间来获取所有信息,那么您可以提供另一个viewcontroller,其上有splash screen并在那里进行检查/或在appDelegate

答案 1 :(得分:0)

您可以使用MKReverseGeocoder获取MKPlacemark,您可以获得CurrentAddress

的苹果示例代码

或这是另一个链接

objective c how to get placemarks of given coordinates

答案 2 :(得分:0)

CLLocationManager获取当前的latLong,因为你必须使用

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//for iOS 6+
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation 
{
//for lower then iOS 6
}

从lat获取地址并长期使用此网址

    NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?sensor=true&latlng=%f,%f",YourLatitude,YourLongitude];