GPSで人を特定するプログラムに追加しようとしていますが、その人がいる状態に値を設定します。例:GPSは、iphoneから人を特定し、現在の状態を返します。たとえば、カリフォルニア、次に状態変数は、文字列としてカリフォルニアに設定されます。誰かが例を持っているので、助けていただければ幸いです。
4 に答える
Core Locationを使用して場所を検索し、MKReverseGeocoderを使用してその場所から状態を取得できます。
あなたがしなければならないことは、現在の座標を見つける CLLocationManager をセットアップすることです。現在の座標では、MKReverseGeoCoder を使用して現在地を見つける必要があります。
- (void)viewDidLoad { // これにより、現在の場所を見つける CCLocationManager が作成されます CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease]; locationManager.delegate = 自己; locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; [locationManager startUpdatingLocation]; } // このデリゲートは、アプリが現在の場所を正常に検出したときに呼び出されます - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { // これは MKReverseGeocoder を作成し、見つかった座標を使用して目印を見つけます MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate]; geoCoder.delegate = 自己; [geoCoder 開始]; } // このデリゲート メソッドは、現在の場所を特定する際にエラーが発生した場合に呼び出されます - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)エラー { NSLog(@"locationManager:%@ didFailWithError:%@"、マネージャー、エラー); } // このデリゲートは、reverseGeocoder が目印を見つけたときに呼び出されます - (void)reverseGeocoder:(MKReverseGeocoder *)ジオコーダー didFindPlacemark:(MKPlacemark *)placemark { MKPlacemark * myPlacemark = 目印; // 目印を使用して都市名を取得できるようになりました NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressStateKey]; } // このデリゲートは、reversegeocoder が目印を見つけられなかったときに呼び出されます - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)エラー { NSLog(@"reverseGeocoder:%@ didFailWithError:%@"、ジオコーダー、エラー); }
逆ジオコーディングの使用は必要なものであり、簡単です。
CLLocation *location プロパティを保持するクラスが与えられたら、私がプロジェクトの 1 つのために書いたこのコード スナイパーを使用するだけで完了です。
-(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[@"FormattedAddressLines"] よりも d[@"State"] を取得することに関心があることに注意してください。また、リバース ジオコーディングにはインターネット アクセスが必要であり (Web サービスとして実装されます)、ボリューム制限の対象となることにも注意してください。ほとんどの場合、1 分間に数回の呼び出しを超えてはなりません。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;
}
上記のコードのいくつかの修正:
1. kABPersonAddressStateKey を使用すると、都市ではなく州が得られます
2. kABPersonAddressStateKey のコンパイルに問題がある場合は、AddressBook フレームワークをプロジェクトに追加し、以下を含めます。
#import <AddressBook/AddressBook.h>
あなたの.mファイルで