【问题标题】:How to get device location name in iPhone?如何在 iPhone 中获取设备位置名称?
【发布时间】:2012-10-19 06:29:28
【问题描述】:

目前我正在使用 iPhone 应用程序,使用 CLLocationManager 来获取纬度和经度值。 我不知道这个?如何从此纬度和经度值中获取设备位置(地址)名称?请帮帮我

提前致谢

我试过了:

- (void)viewDidLoad
{
    [super viewDidLoad];

    LocationManager = [[CLLocationManager alloc]init];
    LocationManager.delegate=self;
    LocationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [LocationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSString * latitude = [[[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.latitude]autorelease];
    NSString * longitude = [[[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.longitude]autorelease];

    [LocationManager stopUpdatingLocation];

    NSLog(@"latitude:%@",latitude);
    NSLog(@"longitude:%@",longitude);
}

【问题讨论】:

标签: iphone ios gps location


【解决方案1】:
    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation                     *)newLocation fromLocation:(CLLocation *)oldLocation  

    {    
    [manager stopUpdatingLocation];    
    lati = [[NSString alloc] initWithFormat:@"%+.6f", newLocation.coordinate.latitude];    
    NSLog(@"address:%@",lati);

    longi = [[NSString alloc] initWithFormat:@"%+.6f", newLocation.coordinate.longitude];
    NSLog(@"address:%@",longi);

    [geoCoder reverseGeocodeLocation: newLocation completionHandler: ^(NSArray *placemarks, NSError *error) 
     {
         //Get nearby address
         CLPlacemark *placemark = [placemarks objectAtIndex:0];

         //String to hold address
         NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];

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

         address = [[NSString alloc]initWithString:locatedAt];    
         NSLog(@"address:%@",address);
     }];

}

【讨论】:

    【解决方案2】:
    CLGeocoder *ceo = [[CLGeocoder alloc]init];
            CLLocation *loc = [[CLLocation alloc]initWithLatitude:32.00 longitude:21.322];
    
         [ceo reverseGeocodeLocation: loc completionHandler:
         ^(NSArray *placemarks, NSError *error) {
              CLPlacemark *placemark = [placemarks objectAtIndex:0];
             NSLog(@"placemark %@",placemark);
              //String to hold address
              NSString *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);
    
    
    
          }];
    

    【讨论】:

    • 我有一个疑问,现在我在泰米尔纳德邦的哥印拜陀市,但我收到的地址在 Apple Store, San Francisco, 1 Stockton St, San Francisco, CA 94108-5805, United States。
    • 在设备上运行..模拟器位置指向上述地址
    【解决方案3】:

    你必须做一些相反的位置。你很幸运:Apple 提供了一个类来做到这一点。 请参阅CLGeocoder(适用于 iOS >= 5.0)或MKReverseGeocoder(适用于 iOS

    【讨论】:

      【解决方案4】:

      您可以为此使用 Google Maps API(适用于任何 iOS):

      NSString *req = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%.5f,%.5f&sensor=false&language=da", location.latitude, location.longitude];
      NSString *resultString = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
      

      然后您可以使用任何 JSON 库(在本例中为 SBJSON)解析对 NSDictionary 的响应:

      SBJSON *jsonObject = [[SBJSON alloc] init];
      NSError *error = nil;
      NSDictionary *response = [jsonObject objectWithString:resultString error:&error];
      [jsonObject release];
      

      提取地址:

      if (error) {
              NSLog(@"Error parsing response string to NSObject: %@",[error localizedDescription]);
      
          }else{
      
              NSString *status = [response valueForKey:@"status"];
      
      
              if ([status isEqualToString:@"OK"]) {
      
                  NSArray *results = [response objectForKey:@"results"];
      
                  int count = [results count];
      
                  //NSSLog(@"Matches: %i", count);
      
      
                  if (count > 0) {
                      NSDictionary *result = [results objectAtIndex:0];
      
      
                      NSString *address = [result valueForKey:@"formatted_address"];
      
      
      
                  }
      
              }
      
          }
      

      【讨论】:

      • 我已经编辑/缩短了问题并删除了过时的代码。
      【解决方案5】:

      您必须创建一个 CLLocation 对象并将其传递给reverseGeoCoder

      【讨论】:

        【解决方案6】:

        在 iOS 5.0 之前,我们有 MKReverse Geo-coder 类来查找这个..现在它已被弃用..

        我们必须在 Core Location Framework 中使用 CLGeocoder 类

        【讨论】:

          猜你喜欢
          • 2010-11-09
          • 1970-01-01
          • 1970-01-01
          • 2012-06-25
          • 1970-01-01
          • 2022-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多