Google地图方向线错误

时间:2011-08-06 08:44:28

标签: asp.net-mvc-3 google-maps-api-3

我有以下代码在内容页面上创建地图。

<script type="text/javascript">
    var directionDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;

    function init() {
        directionsDisplay = new google.maps.DirectionsRenderer();
        var latlng = new google.maps.LatLng(54.6, -4.25);
        var startlatlng = new google.maps.LatLng(50.067, -5.717);
        var endlatlng = new google.maps.LatLng(58.633, -3.067);
        var myOptions = { zoom: 6, center: latlng, disableDefaultUI: true, mapTypeId: google.maps.MapTypeId.ROADMAP };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        directionsDisplay.setMap(map);
        calcRoute();
        var marker = new google.maps.Marker({ position: startlatlng, map: map, flat: true, title: "Start" });
        var marker = new google.maps.Marker({ position: endlatlng, map: map, flat: true, title: "Finish" });
        var marker = new google.maps.Marker({ position: new google.maps.LatLng(55.067, -4.717), map: map, flat: true, title: "Start" });
    }

    function calcRoute() {
        var startlatlng = new google.maps.LatLng(50.067, -5.717);
        var endlatlng = new google.maps.LatLng(58.633, -3.067);
        var request = {
            origin: startlatlng,
            destination: endlatlng,
            avoidHighways: true,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });
    }
    google.maps.event.addDomListener(window, 'load', init);
</script>

我想要的只是让路线显示在由函数init创建的地图上,但是它以较低的缩放级别重绘它,就好像它忽略了第一张地图一样。它也覆盖了我的开始和结束标记。似乎很少有带有directionService的参数来控制方向线的重叠方式。有人知道发生了什么,因为我没有?

1 个答案:

答案 0 :(得分:0)

似乎这样做的方法是在函数init()的开头添加以下内容:

function init() {
    var rendererOptions = {
        preserveViewport: true
    };
    directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
}