2015/02/10

Swift 使用Google Maps SDK for IOS撰寫iPhone App

先下載Google Maps SDK for IOS並解壓縮,下載同時建立專案,並到Google Play Developer Console將Google Maps SDK for iOS功能開啟,建立一組IOS Key






Google Maps SDK for IOS解壓縮後會看到GoogleMaps-iOS-1.9.1資料夾,將『GoogleMaps.framework』和Resources資料夾內的『GoogleMaps.bundle』複製到專案的『Frameworks』資料夾(沒有Frameworks資料夾須自行建立)


『GoogleMaps.framework』複製到專案時,須將Copy Items if needed打勾,並選擇Create folder references

而『GoogleMaps.bundle』複製到專案時,不需將Copy Items if needed打勾,並選擇Create folder references




加入Objective-C Header,名稱隨意打,程式碼為
#import <GoogleMaps/GoogleMaps.h>




接著到專案檔,Targets/ProjectName/Linked Frameworks and Libraries,新增下方的Frameworks


AVFoundation.framework
CoreData.framework
CoreLocation.framework
CoreText.framework
GLKit.framework
ImageIO.framework
libc++.dylib
libicucore.dylib
libz.dylib
OpenGLES.framework
QuartzCore.framework
SystemConfiguration.framework




在『Project/ProjectName/Build Settings/Other Linker Flags』,加入值『-ObjC』



『Project/Project Name/Build Settings/Objetive-C Bridging Header』輸入自行建立的標頭檔



接著再『AppDelegate.swift』的didFinishLaunchingWithOptions方法內新增下方程式碼
GMSServices.provideAPIKey("Your key")




再來到『ViewController.swift』的viewDidLoad方法內輸入下方程式碼
var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 6)
        var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true
        self.view = mapView
        
        var marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView



再將Main.storyboard的View的Class改為『GMSMapView』,就可以執行看看了




AppDelegate.swift:
//
//  AppDelegate.swift
//  Sample
//
//  Created by C.Y.Fang on 2/10/15.
//  Copyright (c) 2015 C.Y.Fang. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        GMSServices.provideAPIKey("Your key")
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}


ViewController.swift:
//
//  ViewController.swift
//  Sample
//
//  Created by C.Y.Fang on 2/10/15.
//  Copyright (c) 2015 C.Y.Fang. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 6)
        var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true
        self.view = mapView
        
        var marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
    }

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


}


本範例程式碼源自官方Google Maps SDK for iOS/Getting Started