質問するログイン新規登録

回答編集履歴

1

実行したプログラムソースの追加

2020/10/03 06:39

投稿

ryuking
ryuking

スコア7

answer CHANGED
@@ -1,5 +1,110 @@
1
1
  https://developer.apple.com/documentation/mapkit/mkmapview/1452503-convert
2
2
  https://news.mynavi.jp/article/20130123-geofencing/
3
3
  https://github.com/ponpoko1968/GeofencingSampleApp
4
+ https://faboplatform.github.io/SwiftDocs/3.mapkit/013_line/
4
5
 
5
- 上記サイトを参考にさせていただきました。
6
+ 上記サイトを参考にさせていただきました。
7
+
8
+ class ViewController: UIViewController,MKMapViewDelegate {
9
+
10
+ @IBOutlet weak var mapView: MKMapView!
11
+
12
+ // 東京駅
13
+ let latitude = 35.6812362
14
+ let longitude = 139.7649361
15
+
16
+ override func viewDidLoad() {
17
+ super.viewDidLoad()
18
+ // Do any additional setup after loading the view.
19
+ mapView.delegate = self
20
+
21
+ self.initMap()
22
+
23
+ self.drawSquare()
24
+ }
25
+
26
+ func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
27
+
28
+ if annotation is MKUserLocation {
29
+ return nil
30
+ }
31
+ let reuseId = "pin"
32
+ var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
33
+ if pinView == nil {
34
+ pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
35
+ }
36
+ else {
37
+ pinView?.annotation = annotation
38
+ }
39
+
40
+ return pinView
41
+ }
42
+
43
+ func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
44
+ // rendererを生成.
45
+ let myPolyLineRendere: MKPolylineRenderer = MKPolylineRenderer(overlay: overlay)
46
+
47
+ // 線の太さを指定.
48
+ myPolyLineRendere.lineWidth = 5
49
+
50
+ // 線の色を指定.
51
+ myPolyLineRendere.strokeColor = UIColor.red
52
+
53
+ return myPolyLineRendere
54
+ }
55
+
56
+ func drawingPin(){
57
+
58
+ let center:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
59
+
60
+ let region:MKCoordinateRegion = MKCoordinateRegion(center: center,latitudinalMeters: 1000.0,longitudinalMeters: 1000.0)
61
+ self.mapView.setRegion(region, animated: true)
62
+
63
+ //ピンを追加
64
+ let annotation:MKPointAnnotation = MKPointAnnotation.init()
65
+ annotation.coordinate = center
66
+ self.mapView.addAnnotation(annotation)
67
+
68
+ }
69
+
70
+ func drawSquare(){
71
+
72
+ let point_1 = CLLocationCoordinate2D(latitude: latitude - 0.001, longitude: longitude - 0.001)
73
+ let point_2 = CLLocationCoordinate2D(latitude: latitude - 0.001, longitude: longitude + 0.001)
74
+ let point_3 = CLLocationCoordinate2D(latitude: latitude + 0.001, longitude: longitude + 0.001)
75
+ let point_4 = CLLocationCoordinate2D(latitude: latitude + 0.001, longitude: longitude - 0.001)
76
+
77
+ var line1 = [point_1,point_2]
78
+ var line2 = [point_2,point_3]
79
+ var line3 = [point_3,point_4]
80
+ var line4 = [point_4,point_1]
81
+
82
+ let myPolyLine_1: MKPolyline = MKPolyline(coordinates: &line1, count: line1.count)
83
+ let myPolyLine_2: MKPolyline = MKPolyline(coordinates: &line2, count: line2.count)
84
+ let myPolyLine_3: MKPolyline = MKPolyline(coordinates: &line3, count: line3.count)
85
+ let myPolyLine_4: MKPolyline = MKPolyline(coordinates: &line4, count: line4.count)
86
+
87
+ // mapViewにcircleを追加.
88
+ mapView.addOverlay(myPolyLine_1)
89
+ mapView.addOverlay(myPolyLine_2)
90
+ mapView.addOverlay(myPolyLine_3)
91
+ mapView.addOverlay(myPolyLine_4)
92
+
93
+ }
94
+
95
+ func initMap(){
96
+
97
+ // 緯度・軽度を設定
98
+ let location = CLLocationCoordinate2DMake(latitude, longitude)
99
+ // マップビューに緯度・軽度を設定
100
+ mapView.setCenter(location, animated:true)
101
+
102
+ // 縮尺を設定
103
+ var region = mapView.region
104
+ region.center = location
105
+ region.span.latitudeDelta = 0.01
106
+ region.span.longitudeDelta = 0.01
107
+ // マップビューに縮尺を設定
108
+ mapView.setRegion(region, animated:true)
109
+
110
+ }