Google 地图 V3 - 保存到数据库

发布于 2024-11-07 09:06:18 字数 1911 浏览 0 评论 0原文

我想将距路线的距离存储在访问数据库中,我快到了,但只有最后一个距离被提交到数据库中。你能帮助我吗? 只有一个地址作为出发地,我正在尝试使用四个地址作为目的地。不管怎样,谢谢,比尔。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皇甫轩 2024-11-14 09:06:18

我怀疑您在 使用循环和闭包时遇到了 JavaScript 中的常见问题。根据您的描述,实际上只使用循环中的最后一个值。 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);
}

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 variable destino always refers to the last value in the loop.

Try using a closure to encapsulate the code to invoke directionsService.route, like below:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文