如何从iphone GPS查找州位置?

时间:2009-11-23 20:19:52

标签: iphone geocoding core-location reverse-geocoding

我正在尝试添加到我的程序中,该程序可以在GPS中定位人员但是设置了值 人就是这样的状态:GPS从他/她的iPhone找到人然后返回他们所处的状态所以说它加利福尼亚然后状态变量设置为加利福尼亚作为字符串将有人有一个例子任何帮助表示赞赏谢谢!

4 个答案:

答案 0 :(得分:2)

您可以使用Core Location查找位置,然后使用MKReverseGeocoder从该位置获取状态。

答案 1 :(得分:0)

您需要做的是设置一个可以找到当前坐标的CLLocationManager。使用当前坐标,您需要使用MKReverseGeoCoder来查找您的位置。

- (void)viewDidLoad 
{  
    // this creates the CCLocationManager that will find your current location
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];
}

// this delegate is called when the app successfully finds your current location
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    // this creates a MKReverseGeocoder to find a placemark using the found coordinates
    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    geoCoder.delegate = self;
    [geoCoder start];
}

// this delegate method is called if an error occurs in locating your current location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
 NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);
}
// this delegate is called when the reverseGeocoder finds a placemark
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    MKPlacemark * myPlacemark = placemark;
    // with the placemark you can now retrieve the city name
    NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressStateKey];
}

// this delegate is called when the reversegeocoder fails to find a placemark
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
}

答案 2 :(得分:0)

对上述代码进行一些修正:
1.使用kABPersonAddressStateKey将为您提供州,而不是城市 2.如果您在编译kABPersonAddressStateKey时遇到问题,请将AddressBook框架添加到您的项目中并包括:

#import <AddressBook/AddressBook.h> 
你的.m文件中的

答案 3 :(得分:0)

使用反向地理编码是您所需要的,而且很简单。

给定一个包含CLLocation * location属性的类,只需使用我为我的一个项目编写的代码狙击手,就完成了。

-(void)loadAddress:(void (^)(NSError *error))completion {
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:self.location
                   completionHandler:^(NSArray *placemarks, NSError *error) {

                       NSString *address = nil;

                       if (error) {
                           self.placemark = nil;
                           self.address = NSLocalizedString(@"Unknown address.",@"");
                       }
                       else {                          
                           CLPlacemark * placeMark = [placemarks firstObject];

                           self.placemark = placeMark;

                           NSDictionary *d = placeMark.addressDictionary;
                           address = [(NSArray*)d[@"FormattedAddressLines"] componentsJoinedByString:@" "];
                           self.address = address;
                       }

                       // Call caller
                       if (completion) completion(error);
                   }];

}

请注意,您将更感兴趣获得d [@“State”]而不是d [@“FormattedAddressLines”]。 另请注意,反向地理编码需要Internet访问(它实现为Web服务)并受到数量限制。最有可能的是,每分钟不应超过几个电话。如果超过Apple设定的配额,您将收到错误。

为方便起见,这里是placeMark.addressDictionary proprerty存储的KV:

{
    City = Millbrae;
    Country = "Etats-Unis";
    CountryCode = US;
    FormattedAddressLines =     (
        "I-280 N",
        "Half Moon Bay, CA  94019",
        "Etats-Unis"
    );
    Name = "I-280 N";
    State = CA;
    Street = "I-280 N";
    SubAdministrativeArea = "San Mat\U00e9o";
    SubLocality = "Bay Area";
    Thoroughfare = "I-280 N";
    ZIP = 94019;
}