2011-06-28 47 views
0

我使用以下腳本來獲取兩個托盤之間的駕駛距離。將值從javascript傳遞給jsp頁面

<script type="text/javascript"> 

    var geocoder, location1, location2, gDir; 

    function initialize() { 
     geocoder = new GClientGeocoder(); 
     gDir = new GDirections(); 
     GEvent.addListener(gDir, "load", function() { 
      var drivingDistanceMiles = gDir.getDistance().meters/1609.344; 
      var drivingDistanceKilometers = gDir.getDistance().meters/1000; 
      document.getElementById('results').innerHTML = '<strong>Address 1: </strong>' + location1.address + ' (' + location1.lat + ':' + location1.lon + ')<br /><strong>Address 2: </strong>' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')<br /><strong>Driving Distance: </strong>' + drivingDistanceMiles + ' miles (or ' + drivingDistanceKilometers + ' kilometers)'; 
     }); 
    } 

    function showLocation() { 
     geocoder.getLocations(document.forms[0].address1.value, function (response) { 
      if (!response || response.Status.code != 200) 
      { 
       alert("Sorry, we were unable to geocode the first address"); 
      } 
      else 
      { 
       location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address}; 
       geocoder.getLocations(document.forms[0].address2.value, function (response) { 
        if (!response || response.Status.code != 200) 
        { 
         alert("Sorry, we were unable to geocode the second address"); 
        } 
        else 
        { 
         location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address}; 
         gDir.load('from: ' + location1.address + ' to: ' + location2.address); 
        } 
       }); 
      } 
     }); 
    } 

    </script> 

我想VAR drivingDistanceKilometers的值存儲在數據庫中,所以我想這個值傳遞給另一個或相同的JSP頁面。我怎樣才能做到這一點 ???

在此先感謝。

回答

0

您必須向您的JSP發送一個請求,其中drivingDistanceKilometers作爲請求的參數。請求可以通過changing the location發送,或者通過改變HTML頁面形式的輸入值並提交此表單,或通過向您的JSP發送AJAX請求發送。

+0

在做出表格並提供必要的輸入之後,我想知道在javascript中寫入什麼代碼來傳遞drivingDistanceKilometers作爲參數請求。這樣我就可以在jsp頁面中接受。 – geetika