2016-06-11 6 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

вы можете добавить точки останова в viewDidLoad & didUpdateLocations, чтобы точно знать, где ноль необязательно существует –

+0

Check Если в деле didUpdateLocations делегировать метод, userlocation равен nil или нет –

+0

ошибка находится в просмотре did load() –

ответ

0

Проблема заключается в том, что вы используете вид карты без его инициализации

var mapView: GMSMapView? 

в viewDidLoad

self.mapView!.addGestureRecognizer(longPressRecognizer) 
+0

что мне менять?! –

+0

self.view.addGestureRecognizer (longPressRecognizer) –

+0

tnxxxxxx !!! но я не могу добавить маркер с длинным касанием –

1

Ошибка возникает в методах didUpdateLocations, потому что его, кажется, что иногда мы не получаем местоположение, чтобы его прохождения нолю на там.

Таким образом, вы можете использовать блок catch catch или использовать его. Если давайте синтаксис, чтобы избежать этой проблемы.

+0

didUpdateLocations не вызывается, если у него нет местоположений, и он никогда не может быть nil –