Weitere ähnliche Inhalte Ähnlich wie Movable Type Data API連携!店舗情報を地図に表示させよう! (20) Mehr von Kanako Kobayashi (18) Movable Type Data API連携!店舗情報を地図に表示させよう!1. Movable Type Data API連携!
店舗情報を地図に表⽰させよう!
【Swift&Movable Typeコラボ!】
Swiftビギナーズ勉強会
第20回 at ⽇本マイクロソフト株式会社
14. Data APIにアクセス
// URLオブジェクトの⽣成
var URL = Foundation.URL(string: "http://(mtHost)/(mtPath)/mt-data-api.cgi/v3/search?search=
(keyword_encode!)")
・・・省略
// リンクオブジェクトの⽣成
let req = URLRequest(url: URL!)
// セッション情報を取り出し
let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: OperationQueue.main)
// リクエストをタスクとして登録
let task = session.dataTask(with: req, completionHandler: {
(data , request , error) in
・・・・取得した店舗情報をセットする処理
})
// ダウンロード開始
task.resume()
エンドポイントを指定
15. 2)現在位置からの距離を計算
// 現在位置情報
let now_point: CLLocation = CLLocation(latitude: now_latitude, longitude: now_longitude)
// ⾏き先位置情報
let go_point: CLLocation = CLLocation(latitude: go_latitude, longitude: go_longitude)
// 現在地から⾏き先までの距離
distance = go_point.distance(from: now_point)
CLLocationクラスを利⽤して
現在位置と⾏き先の距離を計算
🔰注意するポイント:緯度経度情報はDouble型で
取り扱う必要があります。
18. // 位置情報を設定
let coordinate = CLLocationCoordinate2DMake(latitude, longitude)
// 表⽰領域を設定
let span = MKCoordinateSpanMake(0.005, 0.005)
let region = MKCoordinateRegionMake(coordinate, span)
// ピンを利⽤するには、MKPointAnnotation
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude)
// 店舗情報をセット
annotation.title = shop_info
// ピンを⽴てる
self.dispMap.setRegion(region, animated:true)
self.dispMap.addAnnotation(annotation)
CLLocationCoordinate2DMakeクラスを
利⽤してピンを⽴てる
3)位置情報からマップにピンを⽴てる!
📱iOS9以降は、⾃由に⾊を変更可能
19. // Cellが選択された際に呼び出されるdelegateメソッド
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// 地図に渡す値を取得
map_latitude = Double(recipeList[indexPath.row].shop_latitude)!
map_longitude = Double(recipeList[indexPath.row].shop_longtude)!
shop_info = recipeList[indexPath.row].name
// 地図に画⾯遷移
performSegue(withIdentifier: "goMap", sender: nil)
}
// 地図に渡す値をセットするメソッド
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let MapViewController:MapViewController = segue.destination as! MapViewController
MapViewController.latitude = map_latitude
MapViewController.longitude = map_longitude
MapViewController.shop_info = shop_info
}
検索画⾯から、地図に情報を引き渡す
3)位置情報からマップにピンを⽴てる!