1

重複の可能性:
ユーザーの現在の都市名を取得するには?

次のメソッドを使用して、現在の位置の緯度と経度を取得しています

- (void)viewDidLoad {
  [super viewDidLoad];
  locationManager = [[CLLocationManager alloc] init];
  locationManager.delegate = self;
  locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
  locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
  [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
  int degrees = newLocation.coordinate.latitude;
  double decimal = fabs(newLocation.coordinate.latitude - degrees);
  int minutes = decimal * 60;
  double seconds = decimal * 3600 - minutes * 60;
  NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                   degrees, minutes, seconds];
  latLabel.text = lat;
  degrees = newLocation.coordinate.longitude;
  decimal = fabs(newLocation.coordinate.longitude - degrees);
  minutes = decimal * 60;
  seconds = decimal * 3600 - minutes * 60;
  NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                     degrees, minutes, seconds];
  longLabel.text = longt;
}

今、私は現在の場所の名前を文字列として取得する必要があります...しかし地図上では必要ありません.どうすればこれを行うことができますか.

4

5 に答える 5

3

この共通関数を使用し、この関数の引数として緯度と経度を渡すだけです。

-(NSString *)getAddressFromLatLon:(double)pdblLatitude withLongitude:(double)pdblLongitude
{
    NSString *urlString = [NSString stringWithFormat:kGeoCodingString,pdblLatitude, pdblLongitude];
    NSError* error;
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
    locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    return [locationString substringFromIndex:6];
}

そしてこれを宣言する

#define kGeoCodingString @"http://maps.google.com/maps/geo?q=%f,%f&output=csv"

この関数は、通過している緯度と経度の住所を返します。

それが役に立てば幸い。

于 2012-07-02T07:03:33.527 に答える
3

あなたはリバースジオコーディングについて尋ねています..この目的のためにMKReverseGeocodingを使用してください..このSOの質問はそれを少し説明しています..

于 2012-07-02T06:45:41.220 に答える
3
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [self.locationManager stopUpdatingLocation];

    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        for (CLPlacemark * placemark in placemarks)
        {
            NSString *addressTxt = [NSString stringWithFormat:@"%@ %@,%@ %@", 
                                    [placemark subThoroughfare],[placemark thoroughfare],
                                    [placemark locality], [placemark administrativeArea]];
            NSLog(@"%@",addressTxt);
        }    
    }];
}
于 2012-07-02T06:48:51.310 に答える
2

Google API を使用してアドレスを取得できます。おそらく、緯度と経度の値をパラメーターとして送信する必要があります。

Googleリバース ジオコーディングを使用する

サンプルの http 呼び出しはこちらhttp://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false

于 2012-07-02T06:47:52.530 に答える
0

おそらく、これはMKReverseGeocoderを使用する必要があるものです

于 2012-07-02T06:45:58.510 に答える