【发布时间】: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