2016-06-11 10 views
-1

私は迅速にロングタッチGoogleマップでマーカーを追加しようとしています!!!!! と私はいくつかのエラーが発生したコードを下に追加!致命的なエラー:予期せぬことにnilをアンラベリングしている間にオプション値(lldb)

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 

     }} 


    } 

You can see error page here !!!!

+0

あなたはnilのオプションが –

+0

チェックが存在する場所を正確に知っているのviewDidLoad&didUpdateLocationsにブレークポイントを追加することができますdidUpdateLocationsデリゲートメソッドで、userlocationがnilであるかどうかを確認します。 –

+0

エラーが表示されました。load() –

答えて

0

問題は、それがのviewDidLoadで

var mapView: GMSMapView? 

を初期化されることなく、マップビューを使用することである

self.mapView!.addGestureRecognizer(longPressRecognizer) 
+0

私は何を変更する必要がありますか? –

+0

self.view.addGestureRecognizer(longPressRecognizer) –

+0

tnxxxxxx !!!しかし、まだ長いタッチでマーカーを追加できません。 –

1

その時々私たちはそこにその通過nilを場所を取得されていませんようですので、エラーがdidUpdateLocations方法で育てられています。

try catchブロックを使用するか、if let構文を使用してこの問題を回避してください。

+0

didUpdateLocationsはロケーションを持たず、決してゼロになることができない限り呼び出されません –

関連する問題