【问题标题】:Google Maps SDK returns iOS error in physical device but works fine in SimulatorGoogle Maps SDK 在物理设备中返回 iOS 错误,但在模拟器中工作正常
【发布时间】:2017-05-17 01:18:17
【问题描述】:

我正在使用 Xcode 8 和 Swift 3,并且我正在尝试使用适用于 iOS 的 Google Maps SDK。我编译了我的代码并在 iOS 模拟器中运行它,它运行良好。但是,在我的物理 iPad 和 iPhone 中,该应用程序无法运行并显示错误消息告诉我:

provideAPIKey: 最多调用一次


我已在 Google 的 API 控制台中启用 API,并尝试仅限制 iOS 的 API 密钥,但没有任何效果。

注意:我在代码中包含了 cmets 来解释这些函数。

 override func viewDidLoad() {
    super.viewDidLoad()

    GMSServices.provideAPIKey("AIzaSyDpyBvqZpZnRZIf-m1HNuh_vjdX3GUlmWM")

//这是错误

    let camera = GMSCameraPosition.camera(withLatitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.longitude)!, zoom: 15.0)

    let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
    ********************************************************************


    self.mapView.isMyLocationEnabled = true

    self.mapView.camera = camera


    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
    // A minimum distance a device must move before update event generated
    locationManager.distanceFilter = 500
    // Request permission to use location service
    locationManager.requestWhenInUseAuthorization()
    // Request permission to use location service when the app is run
    locationManager.requestAlwaysAuthorization()
    // Start the update of user's location
    locationManager.startUpdatingLocation()




    let latitudmia = (locationManager.location?.coordinate.latitude)!
    let longitudmia = (locationManager.location?.coordinate.longitude)!

//解析Json并用数据放置标记

// 我正在使用 swiftyJson 解析 JSON 并通过 for 循环获取数据

     let urlStringdatos = "http://softkitect.tech/sandbox/maps/greetings3.php"



    let url=URL(string: urlStringdatos)!

    let session = URLSession.shared.dataTask(with: url){
        (data,response,error) in


        guard let data = data else{
            print("data was nil?")
            return
        }


        let json = JSON(data: data)
        for index in 0...19 {


            let nombres = json[index]["title"].string
            let direccion = json[index]["descripcion"].string
            let latp = json[index]["latitud"].string
            let lngp = json[index]["longitud"].string




            if(nombres?.isEmpty == false && direccion?.isEmpty == false ){

                let latitudlug = Double(latp!)
                let lagitudlug = Double(lngp!)

                DispatchQueue.main.async
                    {




                        // Creates a marker in the center of the map.
                        let marker = GMSMarker()
                        marker.position = CLLocationCoordinate2D(latitude: lagitudlug!, longitude: latitudlug!)
                        marker.title = nombres
                        print("MArcador latylong")
                        print(latitudlug!)
                         print(lagitudlug!)
                        marker.snippet = direccion
                        marker.map = self.mapView
                        print("MArcador Puestofinal")
                }

            }else{print("no hay lugares")}

        }



        // print(json["results"][0]["name"])
        //print(json["results"]["geometry"][0]["location"]["lat"].string)

    }
    session.resume()


}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {


    let userLocation:CLLocation = locations[0] as! CLLocation
    let longitude = userLocation.coordinate.longitude
    let latitude = userLocation.coordinate.latitude
    //Do What ever you want with it





    print(userLocation.coordinate.longitude)
    print(userLocation.coordinate.latitude)
}

【问题讨论】:

    标签: ios iphone swift xcode google-maps-api-3


    【解决方案1】:

    我认为您应该按照此处有关如何使用 Google Maps API 的说明进行操作:https://developers.google.com/maps/documentation/ios-sdk/start?hl=en-us

    将您的 API 密钥添加到您的 AppDelegate.swift,如下所示:

    添加以下导入语句:

    import GoogleMaps 
    

    将以下内容添加到您的application(_:didFinishLaunchingWithOptions:) 方法,用您的 API 密钥替换 YOUR_API_KEY

    GMSServices.provideAPIKey("YOUR_API_KEY") 
    

    如果您还使用 Places API,再次添加您的密钥,如下所示:

    GMSPlacesClient.provideAPIKey("YOUR_API_KEY")
    

    请将您的provideAPIKey 方法调用放入您的应用委托中的application(_:didFinishLaunchingWithOptions:)

    另外,由于错误提示 provideAPIKey 只能被调用一次,请在您的项目中搜索 provideAPIKey 并确保只有一个调用并且该调用在 application(_:didFinishLaunchingWithOptions:) 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      相关资源
      最近更新 更多