LoginSignup
25
22

More than 5 years have passed since last update.

[Swift]Google Maps SDKの使い方

Last updated at Posted at 2015-10-12

XcodeプロジェクトにGoogle Maps SDKを入れる

まずターミナルを起動

Google Maps SDKを使いたいXcodeのプロジェクトにディレクトリ移動します
次に、そこでpod initというコマンドを打ち込みます

次に、Finderでそのプロジェクトをみると、podfileというファイルが作成されます。
そこを以下のように変更

# Uncomment this line to define a global platform for your project
# platform :ios, '6.0'

target 'SampleGoogleMaps' do
pod ‘GoogleMaps’
end

target 'SampleGoogleMapsTests' do

end

target 'SampleGoogleMapsUITests' do

end

このように編集後、保存します
再びターミナルで、pod installというコマンドを打ちます

Google Maps API Keyを取得

続いて、Google Developer ConsoleからGoogle Maps APIを取得します
以下のURLからアクセス
https://console.developers.google.com/

スクリーンショット 2015-10-08 1.53.38.png

プロジェクト名を入れる

スクリーンショット 2015-10-08 1.54.50.png
アプリ内で使用するGoogle APIの有効化

スクリーンショット 2015-10-08 1.55.21.png
Google Maps SDK for iOSを選択

スクリーンショット 2015-10-08 1.55.32.png
APIを有効にするを選択する

スクリーンショット 2015-10-08 1.55.44.png

続いて左側のサイドバーから認証情報を選択する

スクリーンショット 2015-10-08 1.56.34.png

認証情報を追加を選択

スクリーンショット 2015-10-08 1.56.21.png

APIキーを選択する

スクリーンショット 2015-10-08 1.56.41.png

iOSキーを選択する

スクリーンショット 2015-10-08 1.56.48.png

すると、以下のようにBundle Identifierを聞かれるので、
スクリーンショット 2015-10-08 1.57.15.png

Xcodeの以下の部分からBundle Identifierをコピーして、さきほどの部分に次の写真のようにBundle Identifierを記入して、作成する
スクリーンショット 2015-10-08 1.57.26.png

スクリーンショット 2015-10-08 1.57.52.png
こんな感じでAPIを取得できました
スクリーンショット 2015-10-08 1.58.05.png

Code

Finderでxcodeprojではなく、xcworkspaceの方を開き、以下のようにコードを書きます

AppDelegate.swift
import UIKit
import GoogleMaps

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    let googleMapsAPI = "AIzaSyDZLzLCuap1Uodhk3BJlKpxKhzl6eECWSE"


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        GMSServices.provideAPIKey(googleMapsAPI)
        return true
    }
ViewController.swift
import UIKit
import GoogleMaps

class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let camera = GMSCameraPosition.cameraWithLatitude(35.647305, longitude: 139.73597, zoom: 10)
        let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        self.view = mapView





        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(35.647305,139.73597)
        marker.title = "hoge"
        marker.snippet = "hoge"
        marker.map = mapView
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

最後に

実機に登録する場合、Bitcodeについてのエラーを吐く場合があります。そのときは以下の写真のようにBitcodeをNoに設定してあげます。
スクリーンショット 2015-10-08 2.10.19.png

参考WebSite

https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/googlemap/004-makawo-biao-shi
https://console.developers.google.com/project
https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/06-corelocation/001-xian-zai-wei-zhino-qu-de
http://swift.swift-studying.com/entry/2015/07/12/012132

25
22
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
25
22