<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Direction_Complex.aspx.cs"
    Inherits="Samples.Services.Direction_Complex" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>路线查询</title>
    <style type="text/css">
        #maps
        {
            height: 450px;
        }
    </style>

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=zh-CN"></script>

    <script language="javascript" type="text/javascript">
        var map; //申明地图对象
        var directionDisplay; //申明路线显示对象
        var directionsService; //申明路线查询服务
        var stepDisplay; //申明路线步骤对象
        var markerArray = []; //申明标记数组
        //地图初始化函数
        function initialize() {
            // Instantiate a directions service.
            //创建路线查询服务实例
            directionsService = new google.maps.DirectionsService();

            // Create a map and center it on Manhattan.
            var manhattan = new google.maps.LatLng(40.7711329, -73.9741874);
            var myOptions = {
                zoom: 13,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: manhattan
            }
            map = new google.maps.Map(document.getElementById("maps"), myOptions);

            // Create a renderer for directions and bind it to the map.
            //创建一个路线呈现对象并绑定到地图
            var rendererOptions = {
                map: map
            }
            //实例化路线显示对象
            directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions)

            // Instantiate an info window to hold step text.
            //创建信息窗口实例来显示步骤信息
            stepDisplay = new google.maps.InfoWindow();
            calcRoute();
        }
        //计算路线函数
        function calcRoute() {

            // First, remove any existing markers from the map.
            //首先删除所有的标记
            for (i = 0; i < markerArray.length; i++) {
                markerArray[i].setMap(null);
            }

            // Now, clear the array itself.
            //清空标记数组
            markerArray = [];

            // Retrieve the start and end locations and create
            // a DirectionsRequest using WALKING directions.
            var start = document.getElementById("start").value;
            var end = document.getElementById("end").value;
            var request = {
                origin: start,
                destination: end,
                travelMode: google.maps.DirectionsTravelMode.WALKING
            };

            // Route the directions and pass the response to a
            // function to create markers for each step.
            //根据计算出来的路线并调用步骤显示函数来创建标记
            directionsService.route(request, function(response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    var warnings = document.getElementById("warnings_panel");
                    warnings.innerHTML = "<b>" + response.routes[0].warnings + "</b>";
                    directionsDisplay.setDirections(response);
                    showSteps(response);
                }
            });
        }
        //步骤显示函数
        function showSteps(directionResult) {
            // For each step, place a marker, and add the text to the marker's
            // info window. Also attach the marker to an array so we
            // can keep track of it and remove it when calculating new
            // routes.
            //获取步骤地址数组
            var myRoute = directionResult.routes[0].legs[0];
            //为每一个步骤地址创建标记
            for (var i = 0; i < myRoute.steps.length; i++) {
                var marker = new google.maps.Marker({
                    position: myRoute.steps[i].start_point,
                    map: map
                });
                //显示步骤信息
                attachInstructionText(marker, myRoute.steps[i].instructions);
                markerArray[i] = marker;
            }
        }
        //显示步骤信息函数 当点击标记时打开信息窗口并在其中显示步骤信息
        function attachInstructionText(marker, text) {
            google.maps.event.addListener(marker, 'click', function() {
                // Open an info window when the marker is clicked on,
                // containing the text of the step.
                stepDisplay.setContent(text);
                stepDisplay.open(map, marker);
            });
        }
        //将地图初始化函数绑定到window的load事件
        google.maps.event.addDomListener(window, "load", initialize);
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <b>Start: </b>
        <select id="start" onchange="calcRoute();">
            <option value="penn station, new york, ny">Penn Station</option>
            <option value="grand central station, new york, ny">Grand Central Station</option>
            <option value="625 8th Avenue New York NY 10018">Port Authority Bus Terminal</option>
            <option value="staten island ferry terminal, new york, ny">Staten Island Ferry Terminal</option>
            <option value="101 E 125th Street, New York, NY">Harlem - 125th St Station</option>
        </select>
        <b>End: </b>
        <select id="end" onchange="calcRoute();">
            <option value="260 Broadway New York NY 10007">City Hall</option>
            <option value="W 49th St & 5th Ave, New York, NY 10020">Rockefeller Center</option>
            <option value="moma, New York, NY">MOMA</option>
            <option value="350 5th Ave, New York, NY, 10118">Empire State Building</option>
            <option value="253 West 125th Street, New York, NY">Apollo Theatre</option>
            <option value="1 Wall St, New York, NY">Wall St</option>
        </select>
    </div>
    <div id="maps">
    </div>
    <div id="warnings_panel" style="width: 100%; height: 10%; text-align: center">
    </div>
    </form>
</body>
</html>

posted on 2010-11-15 16:15  WQL.NET  阅读(160)  评论(0编辑  收藏  举报