2015-10-13 70 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)") 
} 

而且这样的作品确定。

相关问题