2011-05-12 73 views
0

我想存儲從訪問數據庫中的路線的距離,我幾乎在那裏,但只有最後的距離被提交到數據庫。你可以幫我嗎? 只有一個地址作爲來源,我試着用四個地址作爲目的地。無論如何,比爾。谷歌地圖V3 - 保存到數據庫

<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Google Maps JavaScript API</title> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> </script> 
</head> 

<body> 

<script type="text/javascript"> 

var directionsService = new google.maps.DirectionsService(); 
var origem; 
var destino; 
var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = Z:\\Projetos\\GoogleMaps\\GoogleMaps.mdb;" 

//Database Connection 
var cn = new ActiveXObject("ADODB.Connection"); 
cn.Open(strConn); 

//Recordset 
var rsOrigem = new ActiveXObject("ADODB.Recordset"); 
var sqlOrigem = "select * from tblOrigem"; 
rsOrigem.Open(sqlOrigem, cn); 

var rsDestino = new ActiveXObject("ADODB.Recordset"); 
var sqlDestino = "select * from tblDestino"; 
rsDestino.Open(sqlDestino, cn); 

rsOrigem.Movefirst(); 
rsDestino.Movefirst(); 

while(!rsDestino.eof) { 
origem = rsOrigem.fields("Endereco").value; 
destino = rsDestino.fields("Endereco").value; 

var myOptions = { 
zoom:7, 
mapTypeId: google.maps.MapTypeId.ROADMAP 
} 

var request = { 
    origin: origem, 
    destination: destino, 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
}; 

directionsService.route(request, function(response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 

    var rsGravar = new ActiveXObject("ADODB.Recordset"); 
    rsGravar.Open("Select * From tblDestino Where Endereco = '" + destino + "'", cn, 1, 3); 

    rsGravar.Edit; 
    rsGravar.fields("Distancia").value = response.routes[0].legs[0].distance.value; 
    rsGravar.Update; 
    } 
}); 

rsDestino.MoveNext(); 
} 

rsOrigem.Close(); 
rsDestino.Close(); 
rsGravar.Close(); 
cn.Close(); 

</script> 
</body> 
</html> 

回答

0

我懷疑你在JavaScript中遇到了一個共同的問題,當using loops and closures。從您的描述中,實際上只使用循環中的最後一個值。 directionsService.route使用回調,並且很可能變量destino始終引用循環中的最後一個值。

嘗試使用封閉封裝代碼來調用directionsService.route,象下面這樣:

while(!rsDestino.eof) { 
    origem = rsOrigem.fields("Endereco").value; 
    destino = rsDestino.fields("Endereco").value; 

    (function(origin, dest){ 
     //use variable 'origin' and 'dest' in the code 
     var myOptions= ... 
     var request = ... 
     directionsService.route .... 
    })(origem, destino); 
}