Ios 致命错误:在展开可选值(lldb)时意外发现nil
我正在尝试添加标记在谷歌地图与长触摸在swift!!!!! 当我添加下面的代码时,会出现一些错误Ios 致命错误:在展开可选值(lldb)时意外发现nil,ios,swift,google-maps,marker,Ios,Swift,Google Maps,Marker,我正在尝试添加标记在谷歌地图与长触摸在swift!!!!! 当我添加下面的代码时,会出现一些错误 class ViewController: UIViewController, CLLocationManagerDelegate, UIGestureRecognizerDelegate { var mapView: GMSMapView? var locationManager = CLLocationManager() var locationMarker: GMSM
class ViewController: UIViewController, CLLocationManagerDelegate, UIGestureRecognizerDelegate {
var mapView: GMSMapView?
var locationManager = CLLocationManager()
var locationMarker: GMSMarker!
override func viewDidLoad() {
super.viewDidLoad()
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.handleLongPress(_:)))
self.mapView!.addGestureRecognizer(longPressRecognizer)
GMSServices.provideAPIKey("AIzaSyBw95wEhcSiSBmPWuYkiK0_IBnZQK-Lm7I")
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error" + error.description)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation = locations.last
let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)
let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
longitude: userLocation!.coordinate.longitude, zoom: 16)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
view = mapView
// marker with optional position
let position = CLLocationCoordinate2DMake(10, 10)
let marker = GMSMarker(position: position)
marker.opacity = 0.6
marker.position = center
marker.title = "Current Location"
marker.snippet = ""
marker.map = mapView
//mapView.clear()
//locationManager.stopUpdatingLocation()
}
func handleLongPress(recognizer: UILongPressGestureRecognizer)
{
if (recognizer.state == UIGestureRecognizerState.Began)
{
let longPressPoint = recognizer.locationInView(self.mapView);
let coordinate = mapView!.projection.coordinateForPoint(longPressPoint )
//Now you have Coordinate of map add marker on that location
}}
}
在didUpdateLocations方法中会出现错误,因为有时我们似乎无法获取位置,所以在那里传递nil
因此,您可以使用try-catch-block或使用If-let语法来避免此问题。问题在于,您使用的地图视图没有初始化
var mapView: GMSMapView?
在viewDidLoad中
self.mapView!.addGestureRecognizer(longPressRecognizer)
您可以在viewDidLoad和didUpdateLocations处添加断点,以确切了解nil可选项存在的位置。如果在didUpdateLocations委托方法中,userlocation为nil或NoteError位于view中,则在加载()时,您正在包装mapView并在其上添加UILongPressGestureRecognitor。您正在didUpdateLocations中初始化mapView。这是你的问题,你会犯错误。使用以下代码:self.view.addgestureRecognitizer(LongPressRecognitizer)didUpdateLocations不会被调用,除非它有位置并且永远不能为零。我应该更改什么?!self.view.addgesturecognizer(longPressRecognizer)var position=坐标var marker=GMSMarker(position:position)marker.title=“Test marker”marker.map=mapview请将此答案标记为true并尝试以下操作::var tempMapSubView=self.views[0]为!GMSMapView marker.map=类型为“ViewController”的tempMapSubViewValue没有成员“视图”