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

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

950閲覧

Swift iOSのMapkit 矩形アノテーションから緯度、経度の取得について

ryuking

総合スコア7

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/09/30 00:21

0

0

SwiftのMapkit上に四角形を描画し、その四隅の緯度、経度を取得したい


SwiftのMapkitで表示した地図上に、四角形を描画しその角に重なる地図の緯度と経度を取得したにのですが、ネット上を検索したのですが開発経験がある方がいらっしゃいましたならアドバイスをお願いしたいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TsukubaDepot

2020/09/30 19:08

四角形を表示する部分はできているのでしょうか。 もし可能であれば、その部分も含めて関連するコードもご提示いただいた方が回答がつきやすいかもしれません。 メソッドとしては https://developer.apple.com/documentation/mapkit/mkmapview/1452503-convert この辺りが参考になるかもしれません。
ponpoko1968

2020/10/01 11:05 編集

TsukubaDepotさんのお示しになったリンクのメソッドで相互変換が可能です。目的によっては変換自体必要ないかも知れません。以前私が書いた記事とサンプルコードがあるので参考にしてみてください。 [O2O実現の本命機能! iOSの「ジオフェンシング」を使ってみよう (1) ジオフェンシングって何? | マイナビニュース] https://news.mynavi.jp/article/20130123-geofencing/ [ponpoko1968/GeofencingSampleApp: Sample implementation of geofencing on iOS.] https://github.com/ponpoko1968/GeofencingSampleApp
ryuking

2020/10/01 22:59

TsukubaDepot さま ponpoko1968 さま お世話になります。 大変参考になりました。 自分の予想では、画面のx軸y軸の座標で描画することばかり考えておりました。Mapkitの緯度経度を元に描画すれば良いことを知り、おかげさまで自分のやりたい機能を追加つることができそうです。
TsukubaDepot

2020/10/01 23:01

もし解決されたようでしたら、ぜひ自己解決(と、できれば簡単でも実装の流れの説明)をしていただければ他の方の参考になるかもしれません。
guest

回答1

0

自己解決

https://developer.apple.com/documentation/mapkit/mkmapview/1452503-convert
https://news.mynavi.jp/article/20130123-geofencing/
https://github.com/ponpoko1968/GeofencingSampleApp
https://faboplatform.github.io/SwiftDocs/3.mapkit/013_line/

上記サイトを参考にさせていただきました。

class ViewController: UIViewController,MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!

// 東京駅
let latitude = 35.6812362
let longitude = 139.7649361

override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. mapView.delegate = self self.initMap() self.drawSquare() } func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if annotation is MKUserLocation { return nil } let reuseId = "pin" var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView if pinView == nil { pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) } else { pinView?.annotation = annotation } return pinView } func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { // rendererを生成. let myPolyLineRendere: MKPolylineRenderer = MKPolylineRenderer(overlay: overlay) // 線の太さを指定. myPolyLineRendere.lineWidth = 5 // 線の色を指定. myPolyLineRendere.strokeColor = UIColor.red return myPolyLineRendere } func drawingPin(){ let center:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) let region:MKCoordinateRegion = MKCoordinateRegion(center: center,latitudinalMeters: 1000.0,longitudinalMeters: 1000.0) self.mapView.setRegion(region, animated: true) //ピンを追加 let annotation:MKPointAnnotation = MKPointAnnotation.init() annotation.coordinate = center self.mapView.addAnnotation(annotation) } func drawSquare(){ let point_1 = CLLocationCoordinate2D(latitude: latitude - 0.001, longitude: longitude - 0.001) let point_2 = CLLocationCoordinate2D(latitude: latitude - 0.001, longitude: longitude + 0.001) let point_3 = CLLocationCoordinate2D(latitude: latitude + 0.001, longitude: longitude + 0.001) let point_4 = CLLocationCoordinate2D(latitude: latitude + 0.001, longitude: longitude - 0.001) var line1 = [point_1,point_2] var line2 = [point_2,point_3] var line3 = [point_3,point_4] var line4 = [point_4,point_1] let myPolyLine_1: MKPolyline = MKPolyline(coordinates: &line1, count: line1.count) let myPolyLine_2: MKPolyline = MKPolyline(coordinates: &line2, count: line2.count) let myPolyLine_3: MKPolyline = MKPolyline(coordinates: &line3, count: line3.count) let myPolyLine_4: MKPolyline = MKPolyline(coordinates: &line4, count: line4.count) // mapViewにcircleを追加. mapView.addOverlay(myPolyLine_1) mapView.addOverlay(myPolyLine_2) mapView.addOverlay(myPolyLine_3) mapView.addOverlay(myPolyLine_4) } func initMap(){ // 緯度・軽度を設定 let location = CLLocationCoordinate2DMake(latitude, longitude) // マップビューに緯度・軽度を設定 mapView.setCenter(location, animated:true) // 縮尺を設定 var region = mapView.region region.center = location region.span.latitudeDelta = 0.01 region.span.longitudeDelta = 0.01 // マップビューに縮尺を設定 mapView.setRegion(region, animated:true) }

投稿2020/10/01 23:05

編集2020/10/03 06:39
ryuking

総合スコア7

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問