Google 地图 IOS SDK 在解包时意外为 nil 在调用当前位置时可选
Google Maps IOS SDK unexpected nil when unwrapping optional when calling for current location
我在使用 Google 地图 IOS SDK 时遇到问题。当我尝试呼叫当前位置时,出现错误 "unexpected nil when unwrapping optional"。似乎在引用 newLocation 变量时我得到了 nil。任何帮助将不胜感激。
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 中编辑方案 -> 禁用金属 Api 将解决问题。
我在使用 Google 地图 IOS SDK 时遇到问题。当我尝试呼叫当前位置时,出现错误 "unexpected nil when unwrapping optional"。似乎在引用 newLocation 变量时我得到了 nil。任何帮助将不胜感激。
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 中编辑方案 -> 禁用金属 Api 将解决问题。