Google Maps IOS SDK展开时意外为零调用当前位置时可选
我在使用Google Maps IOS SDK时遇到问题。当我尝试调用一个当前位置时,我得到了错误“unexpected nil When unwrapping optional”。当引用newLocation变量时,我似乎得到了nil。任何帮助都将不胜感激Google Maps IOS SDK展开时意外为零调用当前位置时可选,ios,xcode,google-maps,swift2,google-maps-sdk-ios,Ios,Xcode,Google Maps,Swift2,Google Maps Sdk Ios,我在使用Google Maps IOS SDK时遇到问题。当我尝试调用一个当前位置时,我得到了错误“unexpected nil When unwrapping optional”。当引用newLocation变量时,我似乎得到了nil。任何帮助都将不胜感激 import UIKit; import GoogleMaps; import CoreLocation; class GoHomeController: UIViewController, CLLocationManagerDeleg
import UIKit;
import GoogleMaps;
import CoreLocation;
class GoHomeController: UIViewController, CLLocationManagerDelegate{
@IBOutlet weak var mapView: GMSMapView! //View holding google map in main.storyboard
private var locationManager = CLLocationManager();
override func viewDidLoad() {
super.viewDidLoad();
let mapview = GMSMapView.mapWithFrame(CGRectZero, camera: GMSCameraPosition.cameraWithLatitude(35, longitude: -65, zoom: 10.0));
locationManager.delegate = self;
locationManager.requestAlwaysAuthorization();
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.startUpdatingLocation();
mapview.myLocationEnabled = true;
mapView = mapview;
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
print("didFailWithError: \(error.description)");
let errorAlert = UIAlertView(title: "Error", message: "Failed to Get Your Location", delegate: nil,cancelButtonTitle: "Ok");
errorAlert.show();
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last as CLLocation!
//print("current position: \(newLocation.coordinate.longitude) , \(newLocation.coordinate.latitude)")
if let location = newLocation{
updateMapFrame(location, zoom: 10.0);
}
}
func updateMapFrame(newLocation: CLLocation, zoom: Float) {
let camera = GMSCameraPosition.cameraWithTarget(newLocation.coordinate, zoom: zoom)
self.mapView.animateToCameraPosition(camera) // Error occurs here
}
}
谢谢大家抽出时间
编辑:我忘了提到GoHomeController只是我项目中的视图控制器。这不是主要的,我有一段通向它的路
编辑#2:在模拟器中启动应用程序后,当我进入回家场景时,就会发生错误。这是一个众所周知的问题。在Xcode to Edit Scheme->Disable Metal Api将解决此问题。在哪行代码上出现错误?在updateMapFrame函数中的self.mapView.animateToCameraPosition(camera)。嘿,谢谢你的回答。我进入产品->编辑方案->我的项目。但是,一旦到了那里,我就找不到禁用金属api的选项。@在“选项”选项卡下的“运行”按钮-->产品名称->编辑方案->金属api验证设置为“禁用”