【问题标题】:save & reload directionService.route response保存并重新加载 directionSservice.route 响应
【发布时间】:2012-06-27 09:28:50
【问题描述】:

我有一个将地址作为用户输入的应用程序。对于给定的输入,我在

现在我需要在谷歌地图中的这些起点和终点之间画线。我使用 Javascript 来做到这一点。对于任何给定的地址,我至少要画 50 条线。由于 OVER_QUERY_LIMIT,我想将此响应作为字符串保存在数据库中,然后将其转换回对象并显示在谷歌地图中。

请注意,我总是只有一个已知地址的列表。这些地址使用 From&To lat&long 保存在数据库中。所以我也可以编写一个 cronjob 来检索给定 fromlat&long & to-lat&long 的响应,就像这样。

var directionsDisplay<?php print $i; ?> = new google.maps.DirectionsRenderer(rendererOptions);  
directionsDisplay<?php print $i; ?>.setMap(map); 
var start<?php print $i; ?> = '<?php echo $address1; ?>'; 
var end<?php print $i; ?> = '<?php echo $address2; ?>'; 
var request<?php print $i; ?> = { 
    origin:start<?php print $i; ?>,  
    destination:end<?php print $i; ?>, 

    travelMode: google.maps.DirectionsTravelMode.DRIVING 

}; 

<?php if(isempty($routestr)) { ?> 
directionsService.route(request<?php print $i; ?>, function(response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
        var routestr = JSON.stringify(response, null, 4); 
        //Saving routestr in database using ajax call
        directionsDisplay<?php print $i; ?>.setDirections(response);
    }
}
<?php } //end of $routestr check 
else { //convert $routestr into response and assign to setDirections method
  ?>
  //PHP $routestr is a JSON object automatically. so eval function will not work.
  //var response1 = eval('(' + <?php print $routestr; ?> + ')');

  var response1 = <?php echo $routestr; ?>; //This works.
  alert(response1.status); //displays OK. So I am assured that this is a JSON object now.
   directionsDisplay<?php print $i; ?>.setDirections(response1); //This still does not work. Map is blank

<?php    }//end of $routestr else check ?>

我在其他部分面临问题。 eval 方法不能正确转换对象。因此,当我调用 setDirections(response1) 时不起作用。

有人可以帮忙吗?我想将我的 JSON 字符串转换回 DirectionsResult 对象

我还检查了https://developers.google.com/maps/documentation/javascript/reference#DirectionsResult。这里有一行“请注意,虽然这个结果是“类 JSON”,但它并不是严格意义上的 JSON,因为它间接包含 LatLng 对象。

我在一个 html 文件中测试了这段代码

var res = new google.maps.DirectionsResult(); //I receive js error This is not a class and cannot be instantiated. Don't understand how they send back this as response in route method call.
res1.status = "OK"; //Becoz of above issue, this will not work
alert(res1.status); //Becoz of above issue, this will not work

但是这个警告信息永远不会被执行。什么都没有显示。不知道为什么。

大家不要尝试这个。这不会成功。

目前,我已经更改了我的代码并实现了超时。我在某处读到,我们每秒最多可以向方向服务发出 5 个请求。如果我们在一秒钟内超过 5 个请求,将返回 OVER_QUERY_LIMIT。

实施超时已减慢最终响应速度。但似乎没有别的办法。不确定将此服务作为业务 api 是否可以解决问题。

【问题讨论】:

    标签: javascript json google-maps-api-3


    【解决方案1】:

    我也有这个问题。但在我的情况下,我需要将那条路线画回来并允许用户拖动路线(与我们发出路线请求时存在的相同属性)并更新 BD 中的 lat lng。如果您只需要在已有的这两个点之间画线,您可以请求一条新路线。如果您有 Over_query_limit 问题,您可以保存 response.routes[0].overview_path 中的 LatLng,并使用将 LatLng 解析为 Path 的简单折线将其绘制回来。当然这不会完全是一个新的路由请求。

    【讨论】:

    • 我试过这个:directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { var str = JSON.stringify(response); var obj = JSON.parse(str);方向Display.setDirections(str);}});并且浏览器在属性 中返回错误:(它的值不是数组)。第 1 行 main.js。所以我相信对象在 JSON.parse(str) 中损坏了。我什至不知道是否允许保存 Google 数据。
    • 我已经尝试过绘制折线。但这不是正确的解决方案,因为有时街道是 L 形的,并且有连续的编号。绘制折线只是一条叠加线,不会与地图上的街道线同步。
    • 我已经试过了。您应该尝试将其作为方向Display.setDirections(obj);但这不起作用,因为 DirectionsResult (obj) 只是一个类似 JSON 的对象,而不是完全的 JSON。所以字符串化和解析并没有真正的帮助。
    • 当您拖动一条由请求组成的线时,会发生一些变化。如果您打印 Response.routes[0].legs[i].steps[i] 的长度,您将意识到对象会在没有向 Google 服务器请求的情况下更改。您可以做的是保存步骤和腿中存在的信息,并保存对象的引用。换句话说,您将模拟一个拖动,但解析您已经拥有的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2020-12-16
    相关资源
    最近更新 更多