如何从 iPhone GPS 查找州位置?

发布于 2024-08-12 13:51:24 字数 143 浏览 5 评论 0原文

我正在尝试添加到我的程序中,该程序可以在 GPS 中找到该人,但将值设置为 该人所在的州,例如:GPS 从他/她的 iphone 中定位该人,然后返回他们所在的州,所以说是加利福尼亚州,然后将州变量设置为加利福尼亚州作为字符串,如果有人有一个示例,我们将不胜感激,谢谢!

I'm trying to add to my program that locates the person in GPS but sets a value to
the State that person is in so example: GPS Locates person from his/her iphone then returns the state they are in so say its California then the state variable gets set to California as a string would someone have an example any help is appreciated thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迟到的我 2024-08-19 13:51:24

您可以使用 核心位置找到位置,然后使用 MKReverseGeocoder 从位置获取状态。

You can use Core Location to find the location and then use MKReverseGeocoder to get the state from the location.

猫卆 2024-08-19 13:51:24

您所要做的就是设置一个 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);
}

What you have to do is setup a CLLocationManager that will find your current coordinates. With the current coordinates you need to use MKReverseGeoCoder to find your location.

- (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);
}
空心空情空意 2024-08-19 13:51:24

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

#import <AddressBook/AddressBook.h> 

在您的 .m 文件中

Just a few corrections for the above code:
1. Using kABPersonAddressStateKey will give you the state, not the city
2. If you have problem with compiling kABPersonAddressStateKey, add the AddressBook framework to your project and include:

#import <AddressBook/AddressBook.h> 

in your .m file

乱了心跳 2024-08-19 13:51:24

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

给定一个包含 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 属性存储的 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;
}

Using reverse geocoding is what you need and is straightforward.

Given a class that holds a CLLocation *location property, simply use this code sniper I wrote for one of my project, and you're done.

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

}

Note that you will be more interested in getting d[@"State"] rather than d[@"FormattedAddressLines"].
Also note that reverse geocoding requires internet access (it's implemented as a Web service) and is subject to volume limitation. Most likely, you shouldn't exceed more than a couple of calls per minute. If you exceed the quota set by Apple, you'll receive an error.

For your convenience, here are the KV stored by the placeMark.addressDictionary proprerty:

{
    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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文