2015-10-13 71 views
0

當我問谷歌兩個座標之間的距離的兩倍展開可選的值。當我需要設置距離值到UILabel,我這樣做:致命錯誤:意外發現零,同時訪問URL

func getDistancia(origenLat:Double, origenLon:Double, destinoLat:Double, destinoLon:Double){ 
    var url:String = "http://maps.googleapis.com/maps/api/directions/json?origin=\(origenLat),\(origenLon)&destination=\(destinoLat),\(destinoLon)&sensor=false&units=metric&mode=driving" 
    startConnection(url) 
} 

func startConnection(url:String){ 
    let urlPath: String = url 
    var url: NSURL = NSURL(string:url)! 
    var request: NSURLRequest = NSURLRequest(URL: url) 
    var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)! 
    connection.start() 
} 

func connection(connection: NSURLConnection!, didReceiveData data: NSData!){ 
    self.data.appendData(data) 
} 

func connectionDidFinishLoading(connection: NSURLConnection!) { 
    var err: NSError 

    var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary //The error pops here 
    var routes: NSArray=jsonResult.valueForKey("routes") as! NSArray 
    var routesDic:NSDictionary=routes[0] as! NSDictionary 
    var legs:NSArray=routesDic.valueForKey("legs") as! NSArray 
    var distanceDic:NSDictionary=legs[0] as! NSDictionary 
    //var valor:String=distanceDic.valueForKey("text") as! String 
    var finalDistanceDic:NSDictionary=distanceDic.valueForKey("distance") as! NSDictionary 
    var valor:String=finalDistanceDic.valueForKey("text") as! String 
    ofiDistanciaLabel.text=valor 

} 

這工作正常第一次。之後,在MapKit實例中,當用戶點擊地圖的任何部分時,我會檢測是否有任何接近用戶點擊的城鎮,並計算與新點之間的距離。我更新了原點和目標點(在我的代碼中稱爲origen和destino),並再次調用getDistancia方法。我做這種方式:

map.removeAnnotations(self.map.annotations) 
     var latSeleccionado=pueblosSeleccionados[0].latitud 
     var lonSeleccionado=pueblosSeleccionados[0].longitud 
     var ofiId=pueblosSeleccionados[0].idOficina 
     var ofiQuery="select * from oficinas where id=\(ofiId!)" 
     badajozDB?.open() 
     let resultOficina2:FMResultSet?=badajozDB?.executeQuery(ofiQuery, withArgumentsInArray: nil) 
     var oficina=Oficina() 
     if(resultOficina2?.next()==true){ 

      oficina.municipio=resultOficina2?.stringForColumn("localidad") 
      oficina.direccion=resultOficina2?.stringForColumn("direccion") 
      oficina.latitud=resultOficina2?.doubleForColumn("latitud") 
      oficina.longitud=resultOficina2?.doubleForColumn("longitud") 
      oficina.telefono=resultOficina2?.stringForColumn("telefono") 
      oficina.fax=resultOficina2?.stringForColumn("fax") 
      oficina.email=resultOficina2?.stringForColumn("correo") 

     } 
     origen?.latitud=latSeleccionado 
     origen?.longitud=lonSeleccionado 
     destino?.latitud=oficina.latitud 
     destino?.longitud=oficina.longitud 
     ofiMunicipioLabel.text=oficina.municipio 
     ofiDireccionLabel.text=oficina.direccion 
     ofiPhoneLabel.text=oficina.telefono 
     ofiFaxLabel.text=oficina.fax 
     ofiEmailLabel.text=oficina.email 
     getDistancia(origen!.latitud!, origenLon: origen!.longitud!, destinoLat: destino!.latitud!, destinoLon: destino!.longitud!) 
     zoomToFit() 
     badajozDB?.close() 

但第二次,我收到「致命錯誤:意外發現零而展開的可選值」的錯誤,我不知道爲什麼會發生。新的座標是好的,URL是好的,SQL語句形成正確......但我收到這個錯誤。

任何人都知道爲什麼會這樣呢?

謝謝。

編輯: 縮放到滿()方法:

func zoomToFit() { 
    var allLocations:[CLLocationCoordinate2D] = [ 
     CLLocationCoordinate2D(latitude: origen!.latitud!, longitude: origen!.longitud!), 
     CLLocationCoordinate2D(latitude: destino!.latitud!, longitude: destino!.longitud!) 
    ] 

    var poly:MKPolygon = MKPolygon(coordinates: &allLocations, count: allLocations.count) 

    self.map.setVisibleMapRect(poly.boundingMapRect, edgePadding: UIEdgeInsetsMake(40.0, 40.0, 40.0, 40.0), animated: false) 
    var locationOrigen = CLLocationCoordinate2D(
     latitude: origen!.latitud!, 
     longitude: origen!.longitud! 
    ) 

    var locationDestino=CLLocationCoordinate2D(latitude: destino!.latitud!, longitude: destino!.longitud!) 
    var annotationDestino=MKPointAnnotation() 
    var annotationOrigen = MKPointAnnotation() 
    annotationOrigen.coordinate = locationOrigen 
    annotationDestino.coordinate=locationDestino 

    map.addAnnotation(annotationOrigen) 
    map.addAnnotation(annotationDestino) 
} 

回答

0

固定它,儘管超準確的描述性錯誤消息。

如果有人在將來同樣的問題:

的問題是,可變數據不會被覆蓋,新的數據被附加新值。

所以,我創建了一個新的NSMutableData對象中的所有程序進入方法連接時間:

func connection(connection: NSURLConnection!, didReceiveData data: NSData!){ 

    self.data=NSMutableData() 

    self.data!.appendData(data) 
    println("Data: \(self.data)") 
} 

而且這樣的作品確定。

相關問題