2016-06-02 6 views
0

Ich habe ein Raster von Koordinaten erstellt, um die Entfernungen zu berechnen und einige Logik Zeile für Zeile anzuwenden. Ich muss den Abstand zwischen Punkt A und Punkt B berechnen, oder, wenn ich einen Punkt C habe, den Abstand zwischen Punkt A und Punkt B und zwischen Punkt B und Punkt C, und addiere die zwei Werte.Entfernungsberechnung mit Callback in .NET Gridview

Ich habe versucht, eine Callback-Funktion zu verwenden, aber der Rückgabewert, der bei der Berechnung der Entfernung zwischen Punkt B und Punkt C empfangen wurde, ist immer das letzte Koordinatenpaar und nicht das der aktuellen Linie. Was ist der Fehler in der angehängten Funktion? Und wie kann ich meinen Anruf weniger "umständlich" machen?

Danke

function GetDistance(img, txtRimborso, hfImportoRimborso, hfLastOfTheDay) { 
    var latA = img.attributes['latA'].value; 
    var lngA = img.attributes['lngA'].value; 
    var latB = img.attributes['latB'].value; 
    var lngB = img.attributes['lngB'].value; 
    var latC = img.attributes['latC'].value; 
    var lngC = img.attributes['lngC'].value; 

    var isLastOfTheDay = hfLastOfTheDay.value; 
    var distance; 

    source = new google.maps.LatLng(latA, lngA); 
    destination = new google.maps.LatLng(latB, lngB); 

    GetDist(source, destination, function() { 
     distanceA = Number(this); 

     if (latC == '' && lngC == '') 
     { 
      console.log('latC = \'\' lngC = \'\''); 
      source = new google.maps.LatLng(latB, lngB); 
      destination = new google.maps.LatLng(latA, lngA); 

      GetDist(source, destination, function() { 
       distanceB = Number(this); 

       var fullDistance = Number(((Math.floor(distanceA/1000))) + Number((Math.floor(distanceB/1000)))); 
       console.log('A: ' + Math.floor(distanceA/1000) + ' B: ' + Math.floor(distanceB/1000) + ' = ' + fullDistance); 

      }); 

     } else if (isLastOfTheDay == 'True') { 
      console.log('isLastOfTheDay: ' + isLastOfTheDay); 
      source = new google.maps.LatLng(latB, lngB); 
      destination = new google.maps.LatLng(latC, lngC); 

      GetDist(source, destination, function() { 
       distanceB = Number(this); 

       var fullDistance = Number(((Math.floor(distanceA/1000))) + Number((Math.floor(distanceB/1000)))); 
       console.log('A: ' + Math.floor(distanceA/1000) + ' B: ' + Math.floor(distanceB/1000) + ' = ' + fullDistance); 

      }); 

     } else { 
      var fullDistance = (Math.floor(distanceA/1000)); 

     } 

    }); 
} 

function GetDist(source, destination, fn) { 
    var service = new google.maps.DistanceMatrixService(); 
    service.getDistanceMatrix({ 
     origins: [source], 
     destinations: [destination], 
     travelMode: google.maps.TravelMode.DRIVING, 
     unitSystem: google.maps.UnitSystem.METRIC, 
     avoidHighways: true, 
     avoidTolls: true 
    }, function (response, status) { 
     if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") { 
      fn.call(response.rows[0].elements[0].distance.value); 
     } 
     else 
      fn.call(0); 
    }); 
} 

Antwort

0

ich die Lösung gefunden haben, Wegpunkt mit

function GetDistance(img, lRimborsoKm, txtRimborso, hfImportoRimborso, hfLastOfTheDay) { 

     var latA = img.attributes['latA'].value; 
     var lngA = img.attributes['lngA'].value; 
     var latB = img.attributes['latB'].value; 
     var lngB = img.attributes['lngB'].value; 
     var latC = img.attributes['latC'].value; 
     var lngC = img.attributes['lngC'].value; 

     var isLastOfTheDay = hfLastOfTheDay.value; 


     if (latC == '' && lngC == '') // A -> B + B -> A 
     { 
      source = new google.maps.LatLng(latA, lngA); 
      destination = new google.maps.LatLng(latA, lngA); 
      var waypts = []; 
      waypts.push({ 
       location: new google.maps.LatLng(latB, lngB), 
       stopover: true 
      }) 
      GetDist(source, destination, waypts, function() { 
       distance = Number(this); 

       var fullDistance = Number(distance); 
      }); 

     } else if (isLastOfTheDay == 'True') { // A -> B + B -> C 

      source = new google.maps.LatLng(latA, lngA); 
      destination = new google.maps.LatLng(latC, lngC); 
      var waypts = []; 
      waypts.push({ 
       location: new google.maps.LatLng(latB, lngB), 
       stopover: true 
      }) 
      GetDist(source, destination, waypts, function() { 
       distance = Number(this); 

       var fullDistance = Number(distance); 
      }); 

     } else { // A -> B 
      source = new google.maps.LatLng(latA, lngA); 
      destination = new google.maps.LatLng(latB, lngB); 
      var waypts = []; 

      GetDist(source, destination, waypts, function() { 
       distance = Number(this); 

       var fullDistance = Number(distance); 
      }); 
     } 
    } 

    function GetDist(source, destination, waypts, fn) { 

     directionsService.route({ 
      origin: source, 
      destination: destination, 
      waypoints: waypts, 
      optimizeWaypoints: true, 
      avoidHighways: true, 
      avoidTolls: true, 
      travelMode: google.maps.TravelMode.DRIVING 
     }, function (response, status) { 
      if (status === google.maps.DirectionsStatus.OK) { 
       var route = response.routes[0]; 
       var distance = 0; 
       for (var i = 0; i < route.legs.length; i++) { 
        var routeSegment = i + 1; 
        distance += Math.floor(route.legs[i].distance.value/1000); 
       } 
       fn.call(distance); 
      } else { 
       fn.call(0); 
       console.log(status); 
       console.log('latA: ' + source.lat() + ', lngA: ' + source.lng() + ', latB: ' + destination.lat() + ',lngB: ' + destination.lng()); 
      } 
     }); 
    } 
Verwandte Themen