2

我可以将来自 Google Maps API Direction Services 的响应保存为 db 中的 JSON 并使用 google.maps.DirectionsRenderer() 重用它在地图上绘制吗?

我已经将它保存在数据库中,但是当我想使用新的 google.maps.DirectionsRenderer() 在地图上重绘时;它没有在地图上显示线。但是,它确实根据我从 db 加载的 JSON 显示方向面板。

下面是代码片段:

            $.ajax({
                type: "GET",
                url: 'controller.php',
                data: {action: 'routedata', id: id},
                dataType: 'json',
                success: function(data){
                    if(data && data.routes && data.routes.length > 0){
                        var thisRouteDirRender = new google.maps.DirectionsRenderer();
                        thisRouteDirRender.setMap(map);
                        thisRouteDirRender.setPanel(document.getElementById('directions-panel'));
                        thisRouteDirRender.setDirections(data);
                    }
                }
            });
4

1 回答 1

1

我认为您可以尝试这样的想法:

$.ajax({
    type: "GET",
    url: 'controller.php',
    data: {action: 'routedata', id: id},
    dataType: 'json',
    success: function(data){
        if(data && data.routes && data.routes.length > 0){
            var thisRouteDirRender = new google.maps.DirectionsRenderer();
            var directionsService = new google.maps.DirectionsService();
            thisRouteDirRender.setMap(map);
            thisRouteDirRender.setPanel(document.getElementById('directions-panel'));
            var request = {
                origin: data.routes[0].LatLng ,
                destination: data.routes[data.routes.length - 1].LatLng,
                travelMode: google.maps.TravelMode.WALKING
            };
            directionsService.route(request, function(result, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    thisRouteDirRender.setDirections(result);
                }
            });
        }
    }
});

您需要使用google.maps.DirectionsService来构建路线。您也可以waypoints[]data. 您可以在谷歌文档中看到的请求方法的所有附加参数

于 2013-07-16T20:01:17.593 回答