English 中文(简体)
How to find State location from iphone GPS?
原标题:

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!

问题回答

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

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

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

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 MatU00e9o";
    SubLocality = "Bay Area";
    Thoroughfare = "I-280 N";
    ZIP = 94019;
}




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签