2012-06-27 66 views
0

我有一个应用程序将地址作为来自用户的输入。对于给定的输入,我在< 2 KMS距离上找到地址列表(保存在数据库中)。我有数据库中地址的长信息。每个地址都有一个从&到点。纬&这些从&到这些点也都在数据库中。保存并重新加载directionService.route响应

现在我需要在这些从&到谷歌地图中的点之间画线。我用Javascript来做到这一点。对于任何给定的地址,我将有至少50行被绘制。由于OVER_QUERY_LIMIT,我想以字符串的形式将此响应保存在数据库中,然后将其转换回对象并在Google地图中显示。

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

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 

测试此代码,但永远不会执行此警报消息。没有显示。不知道为什么。

伙计们不要试试这个。这不会奏效。我的代码&已执行超时。我读了一些地方,我们可以每秒向directionService做出5次请求。如果我们在一秒内超过5次请求,OVER_QUERY_LIMIT将被返回。

执行超时减慢了最终响应速度。但似乎没有其他办法。不知道是否将此服务作为业务API来解决问题。

回答

0

我也有这个问题。但在我的情况下,我需要重新绘制该路线并允许用户拖动路线(与我们提出路线请求时存在的属性相同)并更新BD中的经纬度。如果你只需要画出你已经拥有的这两点之间的界限,那么你可以申请一条新的路线。如果您有Over_query_limit问题,您可以保存response.routes [0] .overview_path中的LatLng,并用简单的Polyline将LatLng解析为路径将其拉回。当然这不会是一个新的路线请求。

+0

我尝试这样做: directionsService.route(请求,功能(响应,状态){ 如果(状态== google.maps.DirectionsStatus.OK){ VAR STR = JSON.stringify(响应); \t var obj = JSON.parse(str); \t directionsDisplay。setDirections(STR); } }); 和浏览器返回属性中的错误:(该值不是数组)。第1行main.js 所以我认为该对象在JSON.parse(str)中被损坏。 我甚至不知道是否允许保存Google数据。 – Daniel

+0

我已经尝试绘制折线。但这不是正确的解决方案,因为有时街道是L形的,并持续不断。绘制多段线只是一个叠加线,不会与地图上的街道线同步。 –

+0

我已经试过了。你应该试试它作为directionsDisplay.setDirections(obj);但是这不起作用,因为DirectionsResult(obj)只是一个类JSON对象而不是完全的JSON。所以stringfying和解析回来并没有真正的帮助。 –