Skip to content

Commit f5792b4

Browse files
author
UQ Times
committed
MKReverseGeocoderを利用したジオコーディングのサンプルを追加
1 parent f72e2bf commit f5792b4

File tree

7 files changed

+549
-46
lines changed

7 files changed

+549
-46
lines changed

CoreLocationSample-MKReverseGeocoder/CoreLocationSample-MKReverseGeocoder.xcodeproj/project.pbxproj

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
CE46E8291573687F0027AFE2 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = CE46E8281573687F0027AFE2 /* ViewController.m */; };
1717
CE46E82C1573687F0027AFE2 /* ViewController_iPhone.xib in Resources */ = {isa = PBXBuildFile; fileRef = CE46E82A1573687F0027AFE2 /* ViewController_iPhone.xib */; };
1818
CE46E82F1573687F0027AFE2 /* ViewController_iPad.xib in Resources */ = {isa = PBXBuildFile; fileRef = CE46E82D1573687F0027AFE2 /* ViewController_iPad.xib */; };
19+
CE46E836157368B90027AFE2 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE46E835157368B90027AFE2 /* CoreLocation.framework */; };
20+
CE46E838157368BE0027AFE2 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE46E837157368BE0027AFE2 /* MapKit.framework */; };
1921
/* End PBXBuildFile section */
2022

2123
/* Begin PBXFileReference section */
@@ -33,13 +35,18 @@
3335
CE46E8281573687F0027AFE2 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
3436
CE46E82B1573687F0027AFE2 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController_iPhone.xib; sourceTree = "<group>"; };
3537
CE46E82E1573687F0027AFE2 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController_iPad.xib; sourceTree = "<group>"; };
38+
CE46E835157368B90027AFE2 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
39+
CE46E837157368BE0027AFE2 /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
40+
CE46E83D1573A21B0027AFE2 /* README.md */ = {isa = PBXFileReference; path = README.md; sourceTree = "<group>"; };
3641
/* End PBXFileReference section */
3742

3843
/* Begin PBXFrameworksBuildPhase section */
3944
CE46E80E1573687F0027AFE2 /* Frameworks */ = {
4045
isa = PBXFrameworksBuildPhase;
4146
buildActionMask = 2147483647;
4247
files = (
48+
CE46E838157368BE0027AFE2 /* MapKit.framework in Frameworks */,
49+
CE46E836157368B90027AFE2 /* CoreLocation.framework in Frameworks */,
4350
CE46E8161573687F0027AFE2 /* UIKit.framework in Frameworks */,
4451
CE46E8181573687F0027AFE2 /* Foundation.framework in Frameworks */,
4552
CE46E81A1573687F0027AFE2 /* CoreGraphics.framework in Frameworks */,
@@ -52,6 +59,7 @@
5259
CE46E8061573687F0027AFE2 = {
5360
isa = PBXGroup;
5461
children = (
62+
CE46E83D1573A21B0027AFE2 /* README.md */,
5563
CE46E81B1573687F0027AFE2 /* CoreLocationSample-MKReverseGeocoder */,
5664
CE46E8141573687F0027AFE2 /* Frameworks */,
5765
CE46E8121573687F0027AFE2 /* Products */,
@@ -69,6 +77,8 @@
6977
CE46E8141573687F0027AFE2 /* Frameworks */ = {
7078
isa = PBXGroup;
7179
children = (
80+
CE46E837157368BE0027AFE2 /* MapKit.framework */,
81+
CE46E835157368B90027AFE2 /* CoreLocation.framework */,
7282
CE46E8151573687F0027AFE2 /* UIKit.framework */,
7383
CE46E8171573687F0027AFE2 /* Foundation.framework */,
7484
CE46E8191573687F0027AFE2 /* CoreGraphics.framework */,
@@ -220,7 +230,7 @@
220230
GCC_WARN_ABOUT_RETURN_TYPE = YES;
221231
GCC_WARN_UNINITIALIZED_AUTOS = YES;
222232
GCC_WARN_UNUSED_VARIABLE = YES;
223-
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
233+
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
224234
SDKROOT = iphoneos;
225235
TARGETED_DEVICE_FAMILY = "1,2";
226236
};
@@ -238,7 +248,7 @@
238248
GCC_WARN_ABOUT_RETURN_TYPE = YES;
239249
GCC_WARN_UNINITIALIZED_AUTOS = YES;
240250
GCC_WARN_UNUSED_VARIABLE = YES;
241-
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
251+
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
242252
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
243253
SDKROOT = iphoneos;
244254
TARGETED_DEVICE_FAMILY = "1,2";

CoreLocationSample-MKReverseGeocoder/CoreLocationSample-MKReverseGeocoder/CoreLocationSample-MKReverseGeocoder-Info.plist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,10 @@
3131
<key>UISupportedInterfaceOrientations</key>
3232
<array>
3333
<string>UIInterfaceOrientationPortrait</string>
34-
<string>UIInterfaceOrientationLandscapeLeft</string>
35-
<string>UIInterfaceOrientationLandscapeRight</string>
3634
</array>
3735
<key>UISupportedInterfaceOrientations~ipad</key>
3836
<array>
3937
<string>UIInterfaceOrientationPortrait</string>
40-
<string>UIInterfaceOrientationPortraitUpsideDown</string>
41-
<string>UIInterfaceOrientationLandscapeLeft</string>
42-
<string>UIInterfaceOrientationLandscapeRight</string>
4338
</array>
4439
</dict>
4540
</plist>

CoreLocationSample-MKReverseGeocoder/CoreLocationSample-MKReverseGeocoder/ViewController.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
//
88

99
#import <UIKit/UIKit.h>
10+
#import <MapKit/MapKit.h>
1011

11-
@interface ViewController : UIViewController
12+
@interface ViewController : UIViewController <MKReverseGeocoderDelegate, UITableViewDelegate, UITableViewDataSource>
13+
14+
@property (retain, nonatomic) IBOutlet MKMapView *mapView;
15+
@property (retain, nonatomic) IBOutlet UITableView *resultTableView;
16+
17+
- (IBAction)convertCoordinate:(UIButton *)sender;
1218

1319
@end

CoreLocationSample-MKReverseGeocoder/CoreLocationSample-MKReverseGeocoder/ViewController.m

Lines changed: 126 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,149 @@
99
#import "ViewController.h"
1010

1111
@interface ViewController ()
12-
12+
@property (nonatomic, retain) MKReverseGeocoder *geocoder;
13+
@property (nonatomic, retain) NSMutableArray *addressesArray;
1314
@end
1415

1516
@implementation ViewController
1617

18+
@synthesize geocoder = _geocoder;
19+
@synthesize addressesArray = _addressesArray;
20+
21+
@synthesize mapView = _mapView;
22+
@synthesize resultTableView = _resultTableView;
23+
1724
- (void)viewDidLoad
1825
{
1926
[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];
2133
}
2234

2335
- (void)viewDidUnload
2436
{
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+
2546
[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];
2761
}
2862

2963
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
3064
{
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以降
35150
}
151+
NSLog(@"subAdministrativeArea: %@", placemark.subAdministrativeArea);
152+
NSLog(@"subLocality: %@", placemark.subLocality);
153+
NSLog(@"subThoroughfare: %@", placemark.subThoroughfare);
154+
NSLog(@"thoroughfare: %@", placemark.thoroughfare);
36155
}
37156

38157
@end

0 commit comments

Comments
 (0)