[英]Google Maps SDK returns iOS error in physical device but works fine in Simulator
我正在使用Xcode 8和Swift 3,並且正在嘗試使用iOS版Google Maps SDK。 我編譯了代碼,並在iOS模擬器中運行了它,它運行完美。 但是,在我的物理iPad和iPhone中,該應用程序將無法運行並顯示錯誤消息:
ProvideAPIKey:最多應調用一次
我已經在Google的API控制台中啟用了API,還嘗試了僅限制iOS的API密鑰,但沒有任何效果。
注意:我已在代碼中包含注釋以解釋功能。
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=zh-CN
將您的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:)
程序委托中的application(_:didFinishLaunchingWithOptions:)
中。
此外,由於錯誤表明只能調用一次provideAPIKey
因此請在您的項目中搜索provideAPIKey
,並確保只有一個調用並且該調用在application(_:didFinishLaunchingWithOptions:)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.