【问题标题】:Get a friendly location name given a set of geo coords?在给定一组地理坐标的情况下获取友好的位置名称?
【发布时间】:2013-08-02 03:31:45
【问题描述】:

对于给定的一组地理坐标,例如通过 Google 地图等网络服务或其他第三方,是否可以获得对最近有意义的地标(例如街道交叉口)的人性化文本描述?我正在使用 MapKit,但我怀疑它是否内置了任何东西Turns out it does.

例如,我正在寻找最近的街道交叉口(实际上这是理想的)或任何类型的“地名”。比如:

E Hastings St and Main St
128 W Cordova St
PNE Fairgrounds

【问题讨论】:

  • 哎呀可能已经有人问过了:stackoverflow.com/questions/7914900/…
  • 这个问题直接要求 MapKit 解决方案,所以我将保持开放状态,因为我对任何类型的 Web 服务解决方案都感兴趣。
  • 哇,伙计...您正在寻找网络服务解决方案?如果您查看我的回答,我认为它可以让您避免使用网络服务的这种需求

标签: iphone web-services geolocation mapkit


【解决方案1】:

所以我做了一个项目来解决我遇到的这个问题......你可以参考苹果开发者网页中的CLPlacemark class reference

这是我使用它的方式,我认为您还必须像我一样将 corelocation 库添加到项目中。

#import <CoreLocation/CoreLocation.h>

现在在.h file:

添加 CLLocation 代表:&lt;CLLocationManagerDelegate&gt;

然后添加以下实例变量:

CLLocationManager *locationManager;
CLLocation *currentLocation;

现在在.m file:

-(void) viewDidLoad{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move, location is updated
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; // get best current locaton coords
    locationManager.headingFilter = 1;
    [locationManager startUpdatingLocation];
    [locationManager startUpdatingHeading];
}

现在在viewDidLoad 方法之后或之前实现CLLocationManager 委托方法,无论您喜欢哪个:

#pragma mark CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    currentLocation = [locations objectAtIndex:0];
    [locationManager stopUpdatingLocation];
    NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
    [geocoder reverseGeocodeLocation:currentLocation
                   completionHandler:^(NSArray *placemarks, NSError *error) {
                       if (error){
                           NSLog(@"Geocode failed with error: %@", error);
                           return;
                       }
                       CLPlacemark *placemark = [placemarks objectAtIndex:0];
                       NSLog(@"City = %@ : State = %@ : Country = %@ : Zip Code = %@", placemark.locality, placemark.administrativeArea, placemark.ISOcountryCode, placemark.postalCode);
                   }];
}

运行代码并确保如果您使用的是 iOS 模拟器,请务必通过单击控制台输出屏幕上方的箭头按钮来模拟位置。

只有在您构建并运行项目后才会显示。

希望这对你有帮助:)

这是我得到的输出:

2013-08-02 20:39:46.935 PowerOneApp[3449:c07] Detected Location : 37.785834, -122.406417
2013-08-02 20:39:49.773 PowerOneApp[3449:c07] City = San Francisco : State = California : Country = US : Zip Code = 94115

编辑 抱歉,当我写这个答案时,我意识到你想要更多的精确坐标。

这里:

#pragma mark CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    currentLocation = [locations objectAtIndex:0];
    [locationManager stopUpdatingLocation];
    NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
    [geocoder reverseGeocodeLocation:currentLocation
                   completionHandler:^(NSArray *placemarks, NSError *error) {
                       if (error){
                           NSLog(@"Geocode failed with error: %@", error);
                           return;
                       }
                       CLPlacemark *placemark = [placemarks objectAtIndex:0];
                       NSLog(@"%@", placemark.addressDictionary);
                       NSLog(@"%@", [placemark.addressDictionary valueForKey:@"Street"]);
                   }];
}

输出如下:

2013-08-02 20:44:26.052 PowerOneApp[3531:c07] Detected Location : 37.785834, -122.406417
2013-08-02 20:44:26.340 PowerOneApp[3531:c07] {
    City = "San Francisco";
    Country = "United States";
    CountryCode = US;
    FormattedAddressLines =     (
        "Apple Store, San Francisco",
        "1800 Ellis St",
        "San Francisco, CA  94115-4004",
        "United States"
    );
    Name = "Apple Store, San Francisco";
    PostCodeExtension = 4004;
    State = California;
    Street = "1800 Ellis St";
    SubAdministrativeArea = "San Francisco";
    SubLocality = "Union Square";
    SubThoroughfare = 1800;
    Thoroughfare = "Ellis St";
    ZIP = 94115;
}
2013-08-02 20:44:26.340 PowerOneApp[3531:c07] 1800 Ellis St

如您所见,有一个名为addressDictionary 的属性返回CLPlacemark 类所拥有的所有属性。所以在这里,如果你熟悉 NSDictionary 对象,我只是先输出整个字典,然后指定要记录的值。

希望这对您的确切问题有所帮助:)

【讨论】:

    【解决方案2】:

    检查 google maps api,我认为这样的东西应该可以工作:

    var geocoder = new google.maps.Geocoder(),
        lat = "12.1234",
        long = "98.7654",
        latlng = new google.maps.LatLng(sLat, sLong);
    
    geocoder.geocode({"latLng":latlng},function(data,status) {
        if(status == google.maps.GeocoderStatus.OK) {
            var add = data[1].formatted_address; //this is the full address
    
            alert(add);
        }
    });
    

    你也可以得到“E Hastings St and Main St”和场地的全名,你只需要在函数中添加更多的东西。查看谷歌地图 api。

    【讨论】:

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