【问题标题】:I want to get the Location name from the Coordinate value in MapKit for iPhone我想从 MapKit for iPhone 中的坐标值中获取位置名称
【发布时间】:2012-09-17 22:28:39
【问题描述】:

我想从坐标值中获取位置名称。这是代码,

- (void)viewWillAppear:(BOOL)animated {  
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 39.281516;
    zoomLocation.longitude= -76.580806;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
    MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];                
    [_mapView setRegion:adjustedRegion animated:YES];      
}

所以,从那个纬度和经度,我想知道那个位置名称。

【问题讨论】:

  • 您好朋友,感谢您的回答,我很感激,但我得到了解决方案,我无法发布答案,因为没有超过 10 名声望。根据 StackOverflow 的规则,我将在 8 小时后发布。通过 LAt & Long 后,我刚刚使用 google map API 获取 Location 字符串。

标签: iphone xcode ios5 mapkit


【解决方案1】:

以下代码适用于 ios5 及以上版本

 CLGeocoder *ceo = [[CLGeocoder alloc]init];
 CLLocation *loc = [[CLLocation alloc]initWithLatitude:32.00 longitude:21.322]; //insert your coordinates

[ceo reverseGeocodeLocation:loc
          completionHandler:^(NSArray *placemarks, NSError *error) {
              CLPlacemark *placemark = [placemarks objectAtIndex:0];
              if (placemark) {


                  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);
              }
              else {
                  NSLog(@"Could not locate");
              }
          }
 ];

【讨论】:

  • 在我看来,这应该是公认的答案。谢谢!
  • 如果没有 if 块,else 部分如何执行?
  • @AppleDelegate 先生在我的情况下 reversegeocodleocaiton 块未执行
  • 您的 CLGeocoder 实例可能为 nil。
【解决方案2】:

这是从当前位置获取地址字符串的块

in .h file

typedef void(^addressCompletionBlock)(NSString *);

-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletionBlock)completionBlock;

in .m file
#pragma mark - Get Address
-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletionBlock)completionBlock
{
    //Example URL
    //NSString *urlString = @"http://maps.googleapis.com/maps/api/geocode/json?latlng=23.033915,72.524267&sensor=true_or_false";

    NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true_or_false",location.coordinate.latitude, location.coordinate.longitude];

    NSError* error;
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
    NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[locationString dataUsingEncoding:NSUTF8StringEncoding]
                                                          options:0 error:NULL];

    NSString *strFormatedAddress = [[[jsonObject valueForKey:@"results"] objectAtIndex:0] valueForKey:@"formatted_address"];
    completionBlock(strFormatedAddress);
}

并调用函数

CLLocation* currentLocation = [[CLLocation alloc] initWithLatitude:[SharedObj.current_Lat floatValue] longitude:[SharedObj.current_Long floatValue]];

    [self getAddressFromLocation:currentLocation complationBlock:^(NSString * address) {
        if(address) {
            NSLog(@"Address:: %@",address);
        }
    }];

【讨论】:

    【解决方案3】:
    -(NSString *)getAddressFromLatLon:(double)pdblLatitude withLongitude:(double)pdblLongitude
    {
      NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",pdblLatitude, pdblLongitude];
      NSError* error;
      NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
       // NSLog(@"%@",locationString);
    
      locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
      return [locationString substringFromIndex:6];
    }
    

    【讨论】:

    【解决方案4】:

    使用这个方法

    CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
    
    [geocoder reverseGeocodeLocation: zoomLocation completionHandler:^(NSArray* placemarks, NSError* error){
    }
     ];
    

    【讨论】:

      【解决方案5】:

      在iOS 5.0 及以后的版本中,可以使用Core Location 框架的CLGeocoder,至于5.0 以下的iOS,Map Kit Framework 的MKReverseGeocoder。祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-18
        • 1970-01-01
        • 2012-12-05
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多