Skip to content

Commit 915db35

Browse files
author
UQ Times
committed
MKMapViewで地図を表示・操作する基本的なサンプルを追加
1 parent 6dd7a21 commit 915db35

File tree

6 files changed

+1735
-32
lines changed

6 files changed

+1735
-32
lines changed

MapKitSample-MapView/MapKitSample-MapView.xcodeproj/project.pbxproj

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
CE46E8A015745BC00027AFE2 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = CE46E89F15745BC00027AFE2 /* ViewController.m */; };
1717
CE46E8A315745BC00027AFE2 /* ViewController_iPhone.xib in Resources */ = {isa = PBXBuildFile; fileRef = CE46E8A115745BC00027AFE2 /* ViewController_iPhone.xib */; };
1818
CE46E8A615745BC00027AFE2 /* ViewController_iPad.xib in Resources */ = {isa = PBXBuildFile; fileRef = CE46E8A415745BC00027AFE2 /* ViewController_iPad.xib */; };
19+
CE46E8AD15745C050027AFE2 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE46E8AC15745C050027AFE2 /* MapKit.framework */; };
20+
CE46E8B3157469E40027AFE2 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE46E8B2157469E40027AFE2 /* CoreLocation.framework */; };
1921
/* End PBXBuildFile section */
2022

2123
/* Begin PBXFileReference section */
@@ -33,13 +35,17 @@
3335
CE46E89F15745BC00027AFE2 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
3436
CE46E8A215745BC00027AFE2 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController_iPhone.xib; sourceTree = "<group>"; };
3537
CE46E8A515745BC00027AFE2 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController_iPad.xib; sourceTree = "<group>"; };
38+
CE46E8AC15745C050027AFE2 /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
39+
CE46E8B2157469E40027AFE2 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
3640
/* End PBXFileReference section */
3741

3842
/* Begin PBXFrameworksBuildPhase section */
3943
CE46E88515745BC00027AFE2 /* Frameworks */ = {
4044
isa = PBXFrameworksBuildPhase;
4145
buildActionMask = 2147483647;
4246
files = (
47+
CE46E8B3157469E40027AFE2 /* CoreLocation.framework in Frameworks */,
48+
CE46E8AD15745C050027AFE2 /* MapKit.framework in Frameworks */,
4349
CE46E88D15745BC00027AFE2 /* UIKit.framework in Frameworks */,
4450
CE46E88F15745BC00027AFE2 /* Foundation.framework in Frameworks */,
4551
CE46E89115745BC00027AFE2 /* CoreGraphics.framework in Frameworks */,
@@ -69,6 +75,8 @@
6975
CE46E88B15745BC00027AFE2 /* Frameworks */ = {
7076
isa = PBXGroup;
7177
children = (
78+
CE46E8AC15745C050027AFE2 /* MapKit.framework */,
79+
CE46E8B2157469E40027AFE2 /* CoreLocation.framework */,
7280
CE46E88C15745BC00027AFE2 /* UIKit.framework */,
7381
CE46E88E15745BC00027AFE2 /* Foundation.framework */,
7482
CE46E89015745BC00027AFE2 /* CoreGraphics.framework */,
@@ -220,7 +228,7 @@
220228
GCC_WARN_ABOUT_RETURN_TYPE = YES;
221229
GCC_WARN_UNINITIALIZED_AUTOS = YES;
222230
GCC_WARN_UNUSED_VARIABLE = YES;
223-
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
231+
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
224232
SDKROOT = iphoneos;
225233
TARGETED_DEVICE_FAMILY = "1,2";
226234
};
@@ -238,7 +246,7 @@
238246
GCC_WARN_ABOUT_RETURN_TYPE = YES;
239247
GCC_WARN_UNINITIALIZED_AUTOS = YES;
240248
GCC_WARN_UNUSED_VARIABLE = YES;
241-
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
249+
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
242250
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
243251
SDKROOT = iphoneos;
244252
TARGETED_DEVICE_FAMILY = "1,2";

MapKitSample-MapView/MapKitSample-MapView/MapKitSample-MapView-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>

MapKitSample-MapView/MapKitSample-MapView/ViewController.h

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

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

11-
@interface ViewController : UIViewController
12+
@interface ViewController : UIViewController <MKMapViewDelegate>
13+
14+
@property (retain, nonatomic) IBOutlet MKMapView *mapView;
15+
@property (retain, nonatomic) IBOutlet UILabel *mapCoordinateLabel;
16+
@property (retain, nonatomic) IBOutlet UILabel *mapPointLabel;
17+
@property (retain, nonatomic) IBOutlet UILabel *mapRegionLabel;
18+
@property (retain, nonatomic) IBOutlet UISwitch *headingSwitch;
19+
20+
- (IBAction)panUp:(UIButton *)sender;
21+
- (IBAction)panRight:(UIButton *)sender;
22+
- (IBAction)panDown:(UIButton *)sender;
23+
- (IBAction)panLeft:(UIButton *)sender;
24+
- (IBAction)sliderChanged:(UISlider *)sender;
25+
- (IBAction)resetSliderValue:(UISlider *)sender;
26+
- (IBAction)goToTokyoTower:(UIButton *)sender;
27+
- (IBAction)headingSwitchChanged:(UISwitch *)sender;
1228

1329
@end

MapKitSample-MapView/MapKitSample-MapView/ViewController.m

Lines changed: 204 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,229 @@
77
//
88

99
#import "ViewController.h"
10+
#import <CoreLocation/CoreLocation.h>
1011

1112
@interface ViewController ()
12-
13+
{
14+
int _lastSliderValue;
15+
}
1316
@end
1417

1518
@implementation ViewController
1619

20+
@synthesize mapView = _mapView;
21+
@synthesize mapCoordinateLabel = _mapCoordinateLabel;
22+
@synthesize mapPointLabel = _mapPointLabel;
23+
@synthesize mapRegionLabel = _mapRegionLabel;
24+
@synthesize headingSwitch = _headingSwitch;
25+
1726
- (void)viewDidLoad
1827
{
1928
[super viewDidLoad];
20-
// Do any additional setup after loading the view, typically from a nib.
29+
30+
// 現在地を利用する (InterfaceBuilderでも指定可能)
31+
_mapView.showsUserLocation = YES;
32+
_mapView.mapType = MKMapTypeHybrid;
33+
[_mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
2134
}
2235

2336
- (void)viewDidUnload
2437
{
38+
_mapView.delegate = nil;
39+
40+
self.mapView = nil;
41+
self.mapCoordinateLabel = nil;
42+
self.mapPointLabel = nil;
43+
self.mapRegionLabel = nil;
44+
self.headingSwitch = nil;
45+
2546
[super viewDidUnload];
26-
// Release any retained subviews of the main view.
47+
}
48+
49+
- (void)dealloc
50+
{
51+
_mapView.delegate = nil;
52+
53+
[_mapView release];
54+
[_mapCoordinateLabel release];
55+
[_mapPointLabel release];
56+
[_mapRegionLabel release];
57+
[_headingSwitch release];
58+
59+
[super dealloc];
2760
}
2861

2962
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
3063
{
31-
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
32-
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
64+
return (interfaceOrientation == UIInterfaceOrientationPortrait);
65+
}
66+
67+
/* 指定された場所が中心になるようにパン */
68+
- (void)mapViewCenterSetX:(CGFloat)x y:(CGFloat)y
69+
{
70+
CLLocationCoordinate2D coord = [_mapView convertPoint:CGPointMake(x, y) toCoordinateFromView:_mapView];
71+
[_mapView setCenterCoordinate:coord animated:YES];
72+
73+
if (_headingSwitch.on) {
74+
_mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
75+
}
76+
}
77+
78+
/* 地図を上にパン */
79+
- (IBAction)panUp:(UIButton *)sender {
80+
CGFloat nextX = _mapView.frame.size.width / 2;
81+
CGFloat nextY = 0;
82+
[self mapViewCenterSetX:nextX y:nextY];
83+
}
84+
85+
/* 地図を右にパン */
86+
- (IBAction)panRight:(UIButton *)sender {
87+
CGFloat nextX = _mapView.frame.size.width;
88+
CGFloat nextY = _mapView.frame.size.height / 2;
89+
[self mapViewCenterSetX:nextX y:nextY];
90+
}
91+
92+
/* 地図を下にパン */
93+
- (IBAction)panDown:(UIButton *)sender {
94+
CGFloat nextX = _mapView.frame.size.width / 2;
95+
CGFloat nextY = _mapView.frame.size.height;
96+
[self mapViewCenterSetX:nextX y:nextY];
97+
}
98+
99+
/* 地図を左にパン */
100+
- (IBAction)panLeft:(UIButton *)sender {
101+
CGFloat nextX = 0;
102+
CGFloat nextY = _mapView.frame.size.height / 2;
103+
[self mapViewCenterSetX:nextX y:nextY];
104+
}
105+
106+
/* スライダーの変更に合わせて、地図を拡大・縮小する */
107+
- (IBAction)sliderChanged:(UISlider *)sender {
108+
NSLog(@"%lf", sender.value);
109+
int value = round(sender.value);
110+
if (value == _lastSliderValue) {
111+
sender.value = _lastSliderValue;
112+
return;
113+
}
114+
115+
// 左で縮小・右で拡大
116+
MKCoordinateRegion region = _mapView.region;
117+
if (value < _lastSliderValue) {
118+
region.span.latitudeDelta /= 2;
119+
region.span.longitudeDelta /= 2;
120+
} else {
121+
region.span.latitudeDelta *= 2;
122+
region.span.longitudeDelta *= 2;
123+
// 最大サイズを超えて拡大してしまうと、落ちるので判定
124+
if (180 < region.span.latitudeDelta) {
125+
region.span.latitudeDelta = 180; // 緯度は180度
126+
}
127+
if (360 < region.span.longitudeDelta) {
128+
region.span.longitudeDelta = 360; // 経度は360度
129+
}
130+
}
131+
132+
[_mapView setRegion:region animated:YES];
133+
_lastSliderValue = value;
134+
}
135+
136+
/* 離されたときに、スライダーをリセットする*/
137+
- (IBAction)resetSliderValue:(UISlider *)sender {
138+
_lastSliderValue = 0;
139+
[sender setValue:0 animated:YES];
140+
}
141+
142+
/* 東京タワーに飛ぶ */
143+
- (IBAction)goToTokyoTower:(UIButton *)sender {
144+
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(35.658608, 139.745396);
145+
MKCoordinateRegion coordRegion = MKCoordinateRegionMakeWithDistance(coord, 500, 500);
146+
[_mapView setRegion:coordRegion animated:YES];
147+
}
148+
149+
/* ユーザトラッキング+ヘディングアップをするかどうか */
150+
- (IBAction)headingSwitchChanged:(UISwitch *)sender {
151+
if (sender.on) {
152+
[_mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
33153
} else {
34-
return YES;
154+
_mapView.userTrackingMode = MKUserTrackingModeNone;
155+
}
156+
}
157+
158+
#pragma mark - MKMapViewDelegate methods
159+
160+
/* 地図が移動しようとするとき */
161+
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
162+
{
163+
NSLog(@"%s", __PRETTY_FUNCTION__);
164+
}
165+
166+
/* 地図が移動し終えた後 */
167+
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
168+
{
169+
NSLog(@"%s", __PRETTY_FUNCTION__);
170+
171+
// 地図座標
172+
MKCoordinateRegion region = [_mapView convertRect:_mapView.frame toRegionFromView:_mapView];
173+
_mapCoordinateLabel.text = [NSString stringWithFormat:@"%lf\n%lf", region.center.latitude, region.center.longitude];
174+
175+
// 表示領域
176+
_mapRegionLabel.text = [NSString stringWithFormat:@"%lf\n%lf", region.span.latitudeDelta, region.span.longitudeDelta];
177+
178+
// 地図点
179+
MKMapPoint point = MKMapPointForCoordinate(region.center);
180+
_mapPointLabel.text = [NSString stringWithFormat:@"%lf\n%lf", point.x, point.y];
181+
}
182+
183+
/* 地図を読み込むとき */
184+
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView
185+
{
186+
NSLog(@"%s", __PRETTY_FUNCTION__);
187+
}
188+
189+
/* 地図を読み終えた後 */
190+
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
191+
{
192+
NSLog(@"%s", __PRETTY_FUNCTION__);
193+
}
194+
195+
/* 地図の読み込みに失敗した場合 */
196+
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
197+
{
198+
NSLog(@"%s | %@", __PRETTY_FUNCTION__, error);
199+
}
200+
201+
/* 位置情報サービスを開始する前 */
202+
- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView
203+
{
204+
NSLog(@"%s", __PRETTY_FUNCTION__);
205+
}
206+
207+
/* 位置情報サービスを停止した */
208+
- (void)mapViewDidStopLocatingUser:(MKMapView *)mapView
209+
{
210+
NSLog(@"%s", __PRETTY_FUNCTION__);
211+
}
212+
213+
/* 位置情報サービスを更新した */
214+
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
215+
{
216+
NSLog(@"%s", __PRETTY_FUNCTION__);
217+
}
218+
219+
/* 位置情報サービスの更新に失敗した */
220+
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
221+
{
222+
NSLog(@"%s | %@", __PRETTY_FUNCTION__, error);
223+
}
224+
225+
/* ユーザトラッキングが変更された */
226+
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated
227+
{
228+
NSLog(@"%s", __PRETTY_FUNCTION__);
229+
230+
// 移動するとユーザートラッキングが解除されるため、スイッチもオフにする
231+
if (_mapView.userTrackingMode == MKUserTrackingModeNone) {
232+
_headingSwitch.on = NO;
35233
}
36234
}
37235

0 commit comments

Comments
 (0)