【问题标题】:ios: What is the efficient and recommended way to reverse geocode?ios:反向地理编码的有效和推荐方法是什么?
【发布时间】:2013-07-31 07:11:00
【问题描述】:

我要反转某个位置的地理代码纬度和经度,并希望获得该位置的地址。我已经通过谷歌网络服务完成了,但这需要时间。 我想知道是否有其他好的和有效的方法。

当前正在调用此服务,

NSString * getAddress = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%@,%@&sensor=true",Lattitude,Longitude];

【问题讨论】:

标签: iphone ios objective-c reverse-geocoding


【解决方案1】:

你可以使用CLGeocoder:

[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error){
    CLPlacemark *placemark = placemarks[0];
    NSLog(@"Found %@", placemark.name);
}];

这仍然需要时间,因为这两种方法都使用网络服务将纬度/经度转换为一个地方

【讨论】:

  • 请注意,您不应在生产代码中使用它。如果出现错误并且地标为 NULL 或空,这将使应用程序崩溃。
【解决方案2】:

试试这个代码。

geoCoder = [[CLGeocoder alloc]init];
    [self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler:

     //Getting Human readable Address from Lat long,,,

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

【讨论】:

  • NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];正是我想要的
【解决方案3】:

看看GLGeocoder。具体reverseGeocodeLocation:completionHandler:

【讨论】:

    【解决方案4】:

    您可以使用 Apple 的 CLGeocoder(CoreLocation 的一部分)。具体来说,– reverseGeocodeLocation:completionHandler: 方法将返回给定坐标的地址数据字典。

    【讨论】:

      【解决方案5】:

      看看this tutorial,或者,如果只是想快速复制一些东西: NSArray *addressOutput; CLLocation *当前位置; //假设这些实例变量

      // Reverse Geocoding
      CLGeocoder *geocoder = [[CLGeocoder alloc] init];
      [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
          NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
          if (error == nil && [placemarks count] > 0) {
              NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:[placemarks count]];
              for (CLPlacemark *placemark in placemarks) {
                  [tempArray addObject:[NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                                        placemark.subThoroughfare, placemark.thoroughfare,
                                        placemark.postalCode, placemark.locality,
                                        placemark.administrativeArea,
                                        placemark.country]];
              }
              addressOutput = [tempArray copy];
          }
          else {
              addressOutput = nil;
              NSLog(@"%@", error.debugDescription);
          }
      }];
      

      基于教程中的代码。

      【讨论】:

        【解决方案6】:

        如果您不想使用 google API,请尝试此代码 - 基本上将纬度和经度输入转换为 ZIP(可以调整为地址)。

        pip install uszipcode
        
        # Import packages
        from uszipcode import SearchEngine
        search = SearchEngine(simple_zipcode=True)
        from uszipcode import Zipcode
        import numpy as np
        
        #define zipcode search function
        def get_zipcode(lat, lon):
            result = search.by_coordinates(lat = lat, lng = lon, returns = 1)
            return result[0].zipcode
        
        #load columns from dataframe
        lat = df_shooting['Latitude']
        lon = df_shooting['Longitude']
        
        #define latitude/longitude for function
        df = pd.DataFrame({'lat':lat, 'lon':lon})
        
        #add new column with generated zip-code
        df['zipcode'] = df.apply(lambda x: get_zipcode(x.lat,x.lon), axis=1)
        
        #print result
        print(df)
        
        #(optional) save as csv
        #df.to_csv(r'zip_codes.csv')
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多