質問編集履歴
1
書式の改善をしました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -177,4 +177,102 @@
|
|
177
177
|
}
|
178
178
|
|
179
179
|
|
180
|
+
```
|
181
|
+
### 試したこと
|
182
|
+
|
183
|
+
```swift
|
184
|
+
import UIKit
|
185
|
+
import MapKit
|
186
|
+
|
187
|
+
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
|
188
|
+
|
189
|
+
@IBOutlet weak var testMapView: MKMapView?
|
190
|
+
|
191
|
+
var testManager:CLLocationManager = CLLocationManager()
|
192
|
+
|
193
|
+
//アノテーション
|
194
|
+
var annotation:MKPointAnnotation!
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
//最初からあるメソッド
|
199
|
+
override func viewDidLoad() {
|
200
|
+
super.viewDidLoad()
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
//デリゲート先に自分を設定する。
|
205
|
+
testManager.delegate = self
|
206
|
+
|
207
|
+
//位置情報の取得を開始する。
|
208
|
+
testManager.startUpdatingLocation()
|
209
|
+
|
210
|
+
//位置情報の利用許可を変更する画面をポップアップ表示する。
|
211
|
+
testManager.requestWhenInUseAuthorization()
|
212
|
+
|
213
|
+
print("testMapView:", testMapView)
|
214
|
+
|
215
|
+
//デリゲート先を自分に設定する。
|
216
|
+
testMapView?.delegate = self
|
217
|
+
|
218
|
+
}
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
//位置情報取得時の呼び出しメソッド
|
223
|
+
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
224
|
+
|
225
|
+
for location in locations {
|
226
|
+
|
227
|
+
//中心座標
|
228
|
+
let center = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
|
229
|
+
|
230
|
+
//表示範囲
|
231
|
+
let span = MKCoordinateSpanMake(0.01, 0.01)
|
232
|
+
|
233
|
+
//中心座標と表示範囲をマップに登録する。
|
234
|
+
let region = MKCoordinateRegionMake(center, span)
|
235
|
+
testMapView?.setRegion(region, animated:true)
|
236
|
+
|
237
|
+
if(annotation == nil) {
|
238
|
+
//初回はマップにピンを格納する。
|
239
|
+
annotation = MKPointAnnotation()
|
240
|
+
annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
|
241
|
+
testMapView?.addAnnotation(annotation)
|
242
|
+
} else {
|
243
|
+
//2回目以降は移動前と後の座標間に直線を引く。
|
244
|
+
|
245
|
+
//始点と終点の座標
|
246
|
+
var lineLocation:[CLLocationCoordinate2D] = [CLLocationCoordinate2D(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude),
|
247
|
+
CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)]
|
248
|
+
|
249
|
+
//2点間に直線を描画する。
|
250
|
+
let line = MKPolyline(coordinates: &lineLocation, count: 2)
|
251
|
+
testMapView?.add(line)
|
252
|
+
|
253
|
+
//ピンの位置を更新する。
|
254
|
+
annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
}
|
259
|
+
|
260
|
+
|
261
|
+
|
262
|
+
//描画メソッド実行時の呼び出しメソッド
|
263
|
+
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
|
264
|
+
let testRender = MKPolylineRenderer(overlay: overlay)
|
265
|
+
|
266
|
+
//直線の幅を設定する。
|
267
|
+
testRender.lineWidth = 3
|
268
|
+
|
269
|
+
//直線の色を設定する。
|
270
|
+
testRender.strokeColor = UIColor.red
|
271
|
+
|
272
|
+
return testRender
|
273
|
+
}
|
274
|
+
}
|
275
|
+
|
276
|
+
|
277
|
+
|
180
278
|
```
|