如何在iPhone中获取设备位置名称?

时间:2012-10-19 06:29:28

标签: iphone ios gps location

目前我在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);
}

6 个答案:

答案 0 :(得分:5)

    -(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);
     }];

}

答案 1 :(得分:5)

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);



      }];

答案 2 :(得分:1)

你有一些反向位置。你很幸运:Apple提供了一个类来做这件事。 请参阅CLGeocoder(适用于iOS> = 5.0)或MKReverseGeocoder(适用于iOS< 5.0)

答案 3 :(得分:1)

您可以使用 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];

然后,您可以使用Any 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"];



            }

        }

    }

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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

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