LoginSignup
4
5

More than 5 years have passed since last update.

Swift: map情報の表示

Last updated at Posted at 2015-12-17

AppleのMapを使った地図の表示ができるようになったのでメモ。


【重要】Swiftで地図情報の表示を行うには以下の2つの要素が必要。

  • 地図で表示したいポイントの指定
  • 表示する広さの指定

手順イメージ:

・MapViewをストーリーボードに設置 (outlet化、delegate化を実施)
・必要なライブラリ:MapKitの導入
・位置(緯度/経度)の指定
・広さ(タテの広さ、ヨコの広さ)の指定


実装:

VewContlolerに以下のように記述。

viewcontler.swift

import MapKit

class ViewController: UIViewController {

 @IBOutlet weak var map: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()        

        //緯度の指定
        var latitude:CLLocationDegrees = Y緯度)

        //経度の指定
        var longtude:CLLocationDegrees = X経度

        //地図の広さ:画面の端から端までの緯度の差 (これを大きくするとズームアウト)
        var latDelta:CLLocationDegrees = 0.01 
        var lonDelta:CLLocationDegrees = 0.01 

        //地図の広さを指定して広さ(span)を割り出す
        var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

        //経度と緯度を指定して位置(location) を割り出す
        var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longtude)

        //region:位置と広さを指定して地図を表示する。
        var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

        //Mapに位置情報を指定して表示。
        map.setRegion(region, animated: true)

    }

実用的にはGoogleMapを使うことが多いだろうけど、
緯度経度の関数の使い方についてはこんなかんじ。

自分の位置をトレースするツールはよくあるものの、
自分用に秘密で作ってみるのも面白いかも(いろいろ怖いけどな)

ではまた!

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5