Skip to content

Instantly share code, notes, and snippets.

@rok-git
Last active March 28, 2023 12:58
Show Gist options
  • Save rok-git/057fec2941c1f1af1955a8517669af30 to your computer and use it in GitHub Desktop.
Save rok-git/057fec2941c1f1af1955a8517669af30 to your computer and use it in GitHub Desktop.
#import <CoreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>
// CoreLocation を使って住所から 経度・緯度を求める。
// 引数は住所。空白を含む場合はクオートすること。
// COMPILE: cc -fobjc-arc -framework Foundation -framework CoreLocation AddressToCoordinate.m -o AddressToCoordinate
int main(int argc, char *argv[])
{
@autoreleasepool{
__block int ret = 0;
if(argc == 1)
return 1;
NSMutableArray *addresses = [NSMutableArray array];
for(int i = 1; i < argc; i++){
[addresses addObject: [NSString stringWithUTF8String: argv[i]]];
}
NSFileHandle *stdOutput = [NSFileHandle fileHandleWithStandardOutput];
for(NSString *address in addresses){
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder
geocodeAddressString: address
completionHandler: ^(NSArray *placemarks, NSError *err){
if(!err){
CLPlacemark *placemark = placemarks[0];
// NSLog(@"%@", placemark.postalAddress);
CLLocationDegrees latitude = placemark.location.coordinate.latitude;
CLLocationDegrees longitude = placemark.location.coordinate.longitude;
NSString *outputString = [NSString stringWithFormat: @"%f,%f\n",latitude,longitude];
[stdOutput writeData: [outputString dataUsingEncoding:NSUTF8StringEncoding]];
}else{
ret++;
NSLog(@"something is wrong");
}
// complete handler はそれ自身で作った RunLoop を明示的に終了する。
CFRunLoopStop(CFRunLoopGetCurrent());
}];
}
// 他の全ての RunLoop が終了すると、入力ソースもなくタイマーも
// 設定されていないメインの RunLoop も実行を終える。
CFRunLoopRun();
return ret; // 失敗した回数を返す
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment