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

これにはGoogleMapsAPIを使用できます(どの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"];



            }

        }

    }
于 2012-10-19T06:34:40.640 に答える
0

CLLocationオブジェクトを作成し、それを reverseGeoCoderに渡す必要があります。

于 2012-10-19T06:34:16.870 に答える
0

iOS 5.0より前では、これを見つけるためのMKReverseGeo-coderクラスがあります。現在は非推奨です。

CoreLocationFrameworkでCLGeocoderクラスを使用する必要があります

于 2012-10-19T06:38:46.707 に答える