質問編集履歴

1

書式の改善をしました。

2018/09/01 09:28

投稿

Cassini
Cassini

スコア9

test CHANGED
File without changes
test CHANGED
@@ -357,3 +357,199 @@
357
357
 
358
358
 
359
359
  ```
360
+
361
+ ### 試したこと
362
+
363
+
364
+
365
+ ```swift
366
+
367
+ import UIKit
368
+
369
+ import MapKit
370
+
371
+
372
+
373
+ class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
374
+
375
+
376
+
377
+ @IBOutlet weak var testMapView: MKMapView?
378
+
379
+
380
+
381
+ var testManager:CLLocationManager = CLLocationManager()
382
+
383
+
384
+
385
+ //アノテーション
386
+
387
+ var annotation:MKPointAnnotation!
388
+
389
+
390
+
391
+
392
+
393
+
394
+
395
+ //最初からあるメソッド
396
+
397
+ override func viewDidLoad() {
398
+
399
+ super.viewDidLoad()
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+ //デリゲート先に自分を設定する。
408
+
409
+ testManager.delegate = self
410
+
411
+
412
+
413
+ //位置情報の取得を開始する。
414
+
415
+ testManager.startUpdatingLocation()
416
+
417
+
418
+
419
+ //位置情報の利用許可を変更する画面をポップアップ表示する。
420
+
421
+ testManager.requestWhenInUseAuthorization()
422
+
423
+
424
+
425
+ print("testMapView:", testMapView)
426
+
427
+
428
+
429
+ //デリゲート先を自分に設定する。
430
+
431
+ testMapView?.delegate = self
432
+
433
+
434
+
435
+ }
436
+
437
+
438
+
439
+
440
+
441
+
442
+
443
+ //位置情報取得時の呼び出しメソッド
444
+
445
+ func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
446
+
447
+
448
+
449
+ for location in locations {
450
+
451
+
452
+
453
+ //中心座標
454
+
455
+ let center = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
456
+
457
+
458
+
459
+ //表示範囲
460
+
461
+ let span = MKCoordinateSpanMake(0.01, 0.01)
462
+
463
+
464
+
465
+ //中心座標と表示範囲をマップに登録する。
466
+
467
+ let region = MKCoordinateRegionMake(center, span)
468
+
469
+ testMapView?.setRegion(region, animated:true)
470
+
471
+
472
+
473
+ if(annotation == nil) {
474
+
475
+ //初回はマップにピンを格納する。
476
+
477
+ annotation = MKPointAnnotation()
478
+
479
+ annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
480
+
481
+ testMapView?.addAnnotation(annotation)
482
+
483
+ } else {
484
+
485
+ //2回目以降は移動前と後の座標間に直線を引く。
486
+
487
+
488
+
489
+ //始点と終点の座標
490
+
491
+ var lineLocation:[CLLocationCoordinate2D] = [CLLocationCoordinate2D(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude),
492
+
493
+ CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)]
494
+
495
+
496
+
497
+ //2点間に直線を描画する。
498
+
499
+ let line = MKPolyline(coordinates: &lineLocation, count: 2)
500
+
501
+ testMapView?.add(line)
502
+
503
+
504
+
505
+ //ピンの位置を更新する。
506
+
507
+ annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
508
+
509
+ }
510
+
511
+
512
+
513
+ }
514
+
515
+ }
516
+
517
+
518
+
519
+
520
+
521
+
522
+
523
+ //描画メソッド実行時の呼び出しメソッド
524
+
525
+ func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
526
+
527
+ let testRender = MKPolylineRenderer(overlay: overlay)
528
+
529
+
530
+
531
+ //直線の幅を設定する。
532
+
533
+ testRender.lineWidth = 3
534
+
535
+
536
+
537
+ //直線の色を設定する。
538
+
539
+ testRender.strokeColor = UIColor.red
540
+
541
+
542
+
543
+ return testRender
544
+
545
+ }
546
+
547
+ }
548
+
549
+
550
+
551
+
552
+
553
+
554
+
555
+ ```