1

我正在尝试在 ASR.NET mvc 中使用 Google 地图 API。我想使用 DirectionsRenderer。我在这里阅读了有关该内容的文档并尝试使用示例但是此地图未在我的站点中呈现。哪里可能有问题?

<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=MY_KEY&sensor=false">
    </script>
    <script>
        var directionDisplay;
        var directionsService = new google.maps.DirectionsService();

        function initialize() {
            directionsDisplay = new google.maps.DirectionsRenderer();
            var mapOptions = {
                zoom: 7,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: new google.maps.LatLng(41.850033, -87.6500523)
            };
            var map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);
            directionsDisplay.setMap(map);
            directionsDisplay.setPanel(document.getElementById('directions-panel'));

            var control = document.getElementById('control');
            control.style.display = 'block';
            map.controls[google.maps.ControlPosition.TOP].push(control);
        }

        function calcRoute() {
            var start = document.getElementById('start').value;
            var end = document.getElementById('end').value;
            var request = {
                origin: start,
                destination: end,
                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', initialize);
    </script>
</head>
<body onload="initialize()">
    <div id="control">
        <strong>Start:</strong>
        <select id="start" onchange="calcRoute();">
            <option value="chicago, il">Chicago</option>
            <option value="st louis, mo">St Louis</option>
        </select>
        <strong>End:</strong>
        <select id="end" onchange="calcRoute();">
            <option value="chicago, il">Chicago</option>
            <option value="st louis, mo">St Louis</option>
        </select>
    </div>
    <div id="map-canvas" style="float: left; width: 70%; height: 100%">
    </div>
    <div id="directions-panel" style="float: right; width: 30%; height: 100%">
    </div>
</body>
4

1 回答 1

0

确保您在脚本中设置了正确的键src="http://maps.googleapis.com/maps/api/js?key=MY_KEY&sensor=false",因为我已经用我自己的键尝试了您的代码并且它可以工作.. 如果您希望calcRoute();在页面加载时调用函数,只需在末尾添加这一行初始化函数 calcRoute();

于 2013-03-28T11:34:27.733 回答