怠慢プログラマーの備忘録

怠慢でナマケモノなプログラマーの備忘録です。

【Swift】GoogleMapSDKを導入する[備忘録]

APIKeyの取得

Firebaseでプロジェクトを作成する。

Firebaseのプロジェクトの作成に関してはXcodeのプロジェクトにFirebaseRTDBを使う時の初期設定を参考に。

プロジェクトを作成したら、GoogleMapsAPIの「キーの取得」ボタンをクリックして対象のプロジェクトを選択するとキーが表示される。

cocoapod でGoogleMapsを設定

    pod 'GoogleMaps'
    pod 'GooglePlaces'

APIKeyをアプリ側に登録する

AppDelegate.swift

import UIKit
import CoreData
import GoogleMaps
import GooglePlaces

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        // GoogleMaps APIKey
        GMSServices.provideAPIKey("先ほど取得したAPIKey")
        // GooglePlace APIKey
        //GMSPlacesClient.provideAPIKey("先ほど取得したAPIKey")
        return true
    }

...

}

info.plistに認証系を登録

これは現在地を取得したい(GPSを使用する)場合のみ適用。 CoreLocationを使用する為にiOSでは、ユーザに使用許可のアラートを表示する必要があります。

Location Always Usage DescriptionLocation When In Use Usage Descriptionを追加

クラスメソッドさんのこちらの記事が参考になります。

GoogleMapに現在地を表示する

xibでUIViewにGMSMapViewクラスを割り当てておく。

MapViewController

import UIKit
import GoogleMaps
import CoreLocation

class MainMapViewController: UIViewController {
    @IBOutlet weak var mapView: GMSMapView!
    @IBOutlet weak var bottomView: UIView!
    @IBOutlet weak var searchButton: UIButton!
    var locationManager: CLLocationManager?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setUpLocationManager()
        self.createMockMarker()
        self.setNavigationBar()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.mapView.bringSubview(toFront: self.searchButton)
        self.view.sendSubview(toBack: self.mapView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Privates
    
    private func setUpLocationManager() {
        self.locationManager = CLLocationManager()
        guard let locationManager = self.locationManager else { return }
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }
       
}

// MARK: - CLLocationManagerDelegate
extension MainMapViewController: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last {
            let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude,
                                                  longitude: location.coordinate.longitude,
                                                  zoom: 15)
            // 現在地にcameraをセットする
            self.mapView?.camera = camera
            self.locationManager?.stopUpdatingLocation()
            self.initializeMapView()
        }
    }
    
}

[改訂新版]Swiftポケットリファレンス (POCKET REFERENCE)

[改訂新版]Swiftポケットリファレンス (POCKET REFERENCE)

詳解 Swift 第4版

詳解 Swift 第4版