谷歌地图v3方向渲染器重新绘制响应

Google Maps v3 Direction Renderer redraw response

本文关键字:绘制 响应 新绘制 方向 v3 谷歌地图      更新时间:2023-09-26

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

我已经把它保存在DB中了,但当我喜欢用新的google.maps.DirectionsRenderer()重新绘制地图时;它并没有在地图上显示线条。然而,它确实显示了基于我从数据库加载的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);
                    }
                }
            });

我想你可以试试这样的东西:

$.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来构建路由。也可以从data中指定waypoints[]。您可以在谷歌文档中看到请求方法的所有附加参数