2017-10-15 50 views
0

我是iOS開發新手,正在嘗試使用Google Maps SDK的簡單應用。我遵循Google Maps Getting Started guide上列出的步驟。谷歌地圖iOS SDK:地圖不加載

我看到了類似的問題: Google Maps iOS SDK Integration not loading maps 問題出在bundle ID。我確信我不是這種情況。

我已經花了3天嘗試從頭開始新的應用程序。

  • 我創建了一個新的API密鑰,添加了我的包ID。

  • 我加入API密鑰:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
        // Override point for customization after application launch. 
        GMSServices.provideAPIKey("MYKEY") 
        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.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0) 
         let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) 
         view = mapView 
    
         let marker = GMSMarker() 
         marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20) 
         marker.title = "Sydney" 
         marker.snippet = "Australia" 
         marker.map = mapView 
        } 
    
    } 
    

我跑它在模擬器和真正的手機。 我得到標記和Google徽標,但沒有地圖。有人能提供一些線索嗎?

+0

您是否已下載GoogleInfo.plist並將其添加到項目中的目標? – Aks

+0

他們在你的代碼中存在問題。一個是你設置地圖的矩形,另一個是你必須添加地圖作爲子視圖。你可以按照這個教程 https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial –

+0

感謝指針 - 我沒有添加.plist,我只是按照谷歌入門指南。我會嘗試你的建議,並會回來.. – kal

回答

0

嗨我發現了問題並解決了問題。 問題出在API密鑰上 - 而不是代碼。 在IOS應用程序之前,我正在嘗試一個示例Android地圖應用程序,並使用相同的控制檯創建新的API密鑰,但未啓用IOS的Google Maps API。

即使SDK for IOS未啓用,開發人員控制檯也允許在API密鑰上添加IOS限制 - 因此,我從來沒有想過我需要做更多的事情。

如果你得到這個錯誤,你已經驗證軟件包ID是當前,檢查以下內容:

  • 轉到https://console.developers.google.com
  • 點擊儀表盤
  • 點擊「啓用API和服務」與+標記在頁面的頂部。
  • 檢查是否啓用了「Google Maps SDK for iOS」。如果不是,啓用它。
  • 重新運行你的應用程序,並享受:)

-Kal

0

請確保您運行的第一次項目之前您生成正確的API密鑰。我面臨類似的問題,並通過使用此鏈接生成並啓用適用於iOS的APIKey(並非JavaScript或Android)來修復它 - APIKey for iOS