gpt4 book ai didi

ios - Google Maps SDK 在物理设备中返回 iOS 错误,但在模拟器中工作正常

转载 作者:行者123 更新时间:2023-11-30 12:47:34 25 4
gpt4 key购买 nike

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

provideAPIKey: should be called at most once

<小时/>

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

注意:我在代码中添加了注释来解释这些功能。

 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)
}

最佳答案

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

Add your API key to your AppDelegate.swift as follows:

Add the following import statement:

import GoogleMaps 

Add the following to your application(_:didFinishLaunchingWithOptions:) method, replacing YOUR_API_KEY with your API key:

GMSServices.provideAPIKey("YOUR_API_KEY") 

If you are also using the Places API, add your key again as shown here:

GMSPlacesClient.provideAPIKey("YOUR_API_KEY")

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

此外,由于错误表明 provideAPIKey 只能调用一次,因此请在您的项目中搜索 provideAPIKey 并确保只有一次调用,并且该调用位于 application(_:didFinishLaunchingWithOptions:).

关于ios - Google Maps SDK 在物理设备中返回 iOS 错误,但在模拟器中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41435194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com