Google 地图 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>
I want to store the distance from a route in an access database, I'm almost there but only the last distance is commited to the database. Can you help me?
There's only one address as origin, and I'm trying with four addresses as destination. Thanks anyway, Bill.
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您在 使用循环和闭包时遇到了 JavaScript 中的常见问题。根据您的描述,实际上只使用循环中的最后一个值。
directionsService.route
使用回调,并且变量destino
很可能始终引用循环中的最后一个值。尝试使用闭包来封装调用
directionsService.route
的代码,如下所示:I doubt that you encountered a common problem in JavaScript when using loops and closures. From your description, actually only the last value in the loop is used.
directionsService.route
uses a callback and most likely the variabledestino
always refers to the last value in the loop.Try using a closure to encapsulate the code to invoke
directionsService.route
, like below: