我正在使用Xcode 8和Swift 3,而我正在尝试使用Google Maps SDK for iOS。我编译了我的代码并在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)
}
答案 0 :(得分:0)
我认为您应该按照有关如何使用Google Maps API的说明进行操作:https://developers.google.com/maps/documentation/ios-sdk/start?hl=en-us
将您的API密钥添加到AppDelegate.swift,如下所示:
添加以下import语句:
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:)
。< / p>