LoginSignup
0
1

More than 3 years have passed since last update.

Google Mapを表示してみる

Last updated at Posted at 2021-05-10

ゴール

Google Map APIを使って、スマホ画面に地図を表示する。
基本的には こちら の記事と 公式サイト を踏襲して実装していく。

ライブラリ管理とは何ぞや

グーグルマップAPIを自分のプロジェクトに導入するときに、「CocoaPods」と言うライブラリ管理ツールを経由してインストールするみたい。

新規でSingle View Appを作成

作成しようと思うと、「Single View App」がない。
こちら によるとXcode12からは、「Single View App」→「app」に変更された模様。
名称の変更だけかと思いきや、構成ファイルも違う。
公式によると AppDelegade.swift にAPIキーの記述が必要とあるので、
こちらこちら を参考に「LifeSycle」を「UiKit App Delegade」に変更、interfaceを「storyboard」に変更してプロジェクトを作成。
すると、「AppDelegade」「ViewController」が出現。
スクリーンショット 2021-05-10 19.31.24.png
スクリーンショット 2021-05-10 19.34.11.png
SceneDelegadeに関しては、必要になったときに以下を参照することにする。

PG実装

あとは こちらの記事公式 を参考に、
1- Podfileの作成(公式の通りGoogle Maps APIは最新版を指定)
2- コマンドラインでCocoaPods経由でGoogle Maps APIをインストール
3- xcworkspaceファイルを起動
4- AppDelegadeにAPIキー追加

AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions 
    launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    GMSServices.provideAPIKey("API Key")
    return true
}

5- ViewControllerにPG追加

ViewController.swift
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: self.view.frame, camera: camera)
    self.view.addSubview(mapView)

    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView
}

成果

Google Mapをスマホ画面上に表示できました。
次は検索欄の追加、指定した場所にピンを指す。的なことを実装したいと思います。
スクリーンショット 2021-05-10 20.12.27.png

0
1
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
0
1