|
9 | 9 | #import "ViewController.h"
|
10 | 10 |
|
11 | 11 | @interface ViewController ()
|
12 |
| - |
| 12 | +@property (nonatomic, retain) MKReverseGeocoder *geocoder; |
| 13 | +@property (nonatomic, retain) NSMutableArray *addressesArray; |
13 | 14 | @end
|
14 | 15 |
|
15 | 16 | @implementation ViewController
|
16 | 17 |
|
| 18 | +@synthesize geocoder = _geocoder; |
| 19 | +@synthesize addressesArray = _addressesArray; |
| 20 | + |
| 21 | +@synthesize mapView = _mapView; |
| 22 | +@synthesize resultTableView = _resultTableView; |
| 23 | + |
17 | 24 | - (void)viewDidLoad
|
18 | 25 | {
|
19 | 26 | [super viewDidLoad];
|
20 |
| - // Do any additional setup after loading the view, typically from a nib. |
| 27 | + |
| 28 | + // 現在地を利用する |
| 29 | + _mapView.showsUserLocation = YES; |
| 30 | + |
| 31 | + // 履歴を保持する配列 |
| 32 | + _addressesArray = [[NSMutableArray alloc] init]; |
21 | 33 | }
|
22 | 34 |
|
23 | 35 | - (void)viewDidUnload
|
24 | 36 | {
|
| 37 | + [_geocoder cancel]; |
| 38 | + _geocoder.delegate = nil; |
| 39 | + |
| 40 | + self.geocoder = nil; |
| 41 | + self.addressesArray = nil; |
| 42 | + |
| 43 | + self.mapView = nil; |
| 44 | + self.resultTableView = nil; |
| 45 | + |
25 | 46 | [super viewDidUnload];
|
26 |
| - // Release any retained subviews of the main view. |
| 47 | +} |
| 48 | + |
| 49 | +- (void)dealloc |
| 50 | +{ |
| 51 | + [_geocoder cancel]; |
| 52 | + _geocoder.delegate = nil; |
| 53 | + |
| 54 | + [_geocoder release]; |
| 55 | + [_addressesArray release]; |
| 56 | + |
| 57 | + [_mapView release]; |
| 58 | + [_resultTableView release]; |
| 59 | + |
| 60 | + [super dealloc]; |
27 | 61 | }
|
28 | 62 |
|
29 | 63 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
|
30 | 64 | {
|
31 |
| - if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { |
32 |
| - return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); |
33 |
| - } else { |
34 |
| - return YES; |
| 65 | + return (interfaceOrientation == UIInterfaceOrientationPortrait); |
| 66 | +} |
| 67 | + |
| 68 | +/* UIMapViewの中心点の座標を元に、逆ジオコーディング */ |
| 69 | +- (IBAction)convertCoordinate:(UIButton *)sender { |
| 70 | + CLLocationCoordinate2D location = [_mapView convertPoint:_mapView.center toCoordinateFromView:_mapView]; |
| 71 | + NSLog(@"%lf, %lf", location.latitude, location.longitude); |
| 72 | + |
| 73 | + self.geocoder = [[[MKReverseGeocoder alloc] initWithCoordinate:location] autorelease]; |
| 74 | + _geocoder.delegate = self; |
| 75 | + [_geocoder start]; |
| 76 | +} |
| 77 | + |
| 78 | +#pragma mark - MKReverseGeocoderDelegate methods |
| 79 | + |
| 80 | +/* エラー発生時 */ |
| 81 | +- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error |
| 82 | +{ |
| 83 | + NSLog(@"%s | %@", __PRETTY_FUNCTION__, error); |
| 84 | + [_addressesArray insertObject:[error localizedDescription] atIndex:0]; |
| 85 | + [_resultTableView reloadData]; |
| 86 | +} |
| 87 | + |
| 88 | +/* ジオコーディング成功時 */ |
| 89 | +- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark |
| 90 | +{ |
| 91 | + // ログに残す |
| 92 | + [self logPlacemark:placemark]; |
| 93 | + // 履歴に追加 |
| 94 | + NSString *formattedAddress = [[placemark.addressDictionary objectForKey:@"FormattedAddressLines"] componentsJoinedByString:@" "]; |
| 95 | + [_addressesArray insertObject:formattedAddress atIndex:0]; // 最新のものを先頭に |
| 96 | + [_resultTableView reloadData]; |
| 97 | +} |
| 98 | + |
| 99 | +#pragma mark - UITableViewDataSource methods |
| 100 | + |
| 101 | +/* テーブルのセクション数 */ |
| 102 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section |
| 103 | +{ |
| 104 | + return [_addressesArray count]; |
| 105 | +} |
| 106 | + |
| 107 | +/* テーブルのセルに表示する内容 */ |
| 108 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
| 109 | +{ |
| 110 | + NSString *identifier = @"History"; |
| 111 | + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; |
| 112 | + if (cell == nil) { |
| 113 | + cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease]; |
| 114 | + } |
| 115 | + |
| 116 | + // 最新のものが上に来るように数字を数える |
| 117 | + cell.textLabel.text = [NSString stringWithFormat:@"住所%d", ([_addressesArray count] - indexPath.row)]; |
| 118 | + cell.detailTextLabel.text = [_addressesArray objectAtIndex:indexPath.row]; |
| 119 | + |
| 120 | + return cell; |
| 121 | +} |
| 122 | + |
| 123 | +#pragma mark - private methods |
| 124 | + |
| 125 | +/* プレースマークの情報をログに残すだけ |
| 126 | + * iOS 4.3と iOS 5.0との差分は以下のURLを参照 |
| 127 | + * http://developer.apple.com/library/ios/#releasenotes/General/iOS50APIDiff/index.html#//apple_ref/doc/uid/TP40011042 |
| 128 | + */ |
| 129 | +- (void)logPlacemark:(MKPlacemark *)placemark |
| 130 | +{ |
| 131 | + NSLog(@"addressDictionary: %@", placemark.addressDictionary); |
| 132 | + NSLog(@"administrativeArea: %@", placemark.administrativeArea); |
| 133 | + if ([placemark respondsToSelector:@selector(areasOfInterest)]) { |
| 134 | + NSLog(@"areasOfInterest: %@", placemark.areasOfInterest); // iOS 5.0以降 |
| 135 | + } |
| 136 | + NSLog(@"country: %@", placemark.country); |
| 137 | + if ([placemark respondsToSelector:@selector(inlandWater)]) { |
| 138 | + NSLog(@"inlandWater: %@", placemark.inlandWater); // iOS 5.0以降 |
| 139 | + } |
| 140 | + NSLog(@"locality: %@", placemark.locality); |
| 141 | + if ([placemark respondsToSelector:@selector(name)]) { |
| 142 | + NSLog(@"name: %@", placemark.name); // iOS 5.0以降 |
| 143 | + } |
| 144 | + if ([placemark respondsToSelector:@selector(ocean)]) { |
| 145 | + NSLog(@"ocean: %@", placemark.ocean); // iOS 5.0以降 |
| 146 | + } |
| 147 | + NSLog(@"postalCode: %@", placemark.postalCode); |
| 148 | + if ([placemark respondsToSelector:@selector(region)]) { |
| 149 | + NSLog(@"region: %@", placemark.region); // iOS 5.0以降 |
35 | 150 | }
|
| 151 | + NSLog(@"subAdministrativeArea: %@", placemark.subAdministrativeArea); |
| 152 | + NSLog(@"subLocality: %@", placemark.subLocality); |
| 153 | + NSLog(@"subThoroughfare: %@", placemark.subThoroughfare); |
| 154 | + NSLog(@"thoroughfare: %@", placemark.thoroughfare); |
36 | 155 | }
|
37 | 156 |
|
38 | 157 | @end
|
0 commit comments