|
7 | 7 | //
|
8 | 8 |
|
9 | 9 | #import "ViewController.h"
|
| 10 | +#import <CoreLocation/CoreLocation.h> |
10 | 11 |
|
11 | 12 | @interface ViewController ()
|
12 |
| - |
| 13 | +{ |
| 14 | + int _lastSliderValue; |
| 15 | +} |
13 | 16 | @end
|
14 | 17 |
|
15 | 18 | @implementation ViewController
|
16 | 19 |
|
| 20 | +@synthesize mapView = _mapView; |
| 21 | +@synthesize mapCoordinateLabel = _mapCoordinateLabel; |
| 22 | +@synthesize mapPointLabel = _mapPointLabel; |
| 23 | +@synthesize mapRegionLabel = _mapRegionLabel; |
| 24 | +@synthesize headingSwitch = _headingSwitch; |
| 25 | + |
17 | 26 | - (void)viewDidLoad
|
18 | 27 | {
|
19 | 28 | [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]; |
21 | 34 | }
|
22 | 35 |
|
23 | 36 | - (void)viewDidUnload
|
24 | 37 | {
|
| 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 | + |
25 | 46 | [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]; |
27 | 60 | }
|
28 | 61 |
|
29 | 62 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
|
30 | 63 | {
|
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]; |
33 | 153 | } 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; |
35 | 233 | }
|
36 | 234 | }
|
37 | 235 |
|
|
0 commit comments