3

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

6 回答 6

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

}
于 2012-10-19T06:40:58.723 回答
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);



      }];
于 2012-10-19T06:51:30.043 回答
1

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

于 2012-10-19T06:34:16.673 回答
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];

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



            }

        }

    }
于 2012-10-19T06:34:40.640 回答
0

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

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

于 2012-10-19T06:38:46.707 回答
0

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

于 2012-10-19T06:34:16.870 回答