简体   繁体   English

Google ASP中的Google地图路线

[英]google maps route in ASP

I need to get a route between 2 location in asp, since I want variables to improve my page navigation. 我需要在ASP中的2个位置之间获得一条路线,因为我希望变量可以改善我的页面导航。 I found an excellent method online that works wonders, but it activates a onclick function to work. 我在网上找到了一种行之有效的出色方法,但是它激活了onclick函数。 I need to reload the page to obtain variables from my database. 我需要重新加载页面以从数据库中获取变量。

The original version is this one: 原始版本是这样的:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script type="text/javascript">
    var source, destination;
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    google.maps.event.addDomListener(window, 'load', function () {
        new google.maps.places.SearchBox(document.getElementById('txtSource'));

        directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': true });
    });

    function GetRoute() {
        var rome = new google.maps.LatLng(41.918357, 12.485029);
        var mapOptions = {
            zoom: 4,
            center: rome
        };
        map = new google.maps.Map(document.getElementById('dvMap'), mapOptions);
        directionsDisplay.setMap(map);
        directionsDisplay.setPanel(document.getElementById('dvPanel'));

        //*********DIRECTIONS AND ROUTE**********************//
        source = document.getElementById("txtSource").value;
        destination = document.getElementById("txtDestination").value;

        var request = {
            origin: source,
            destination: destination,
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });

        //*********DISTANCE AND DURATION**********************//
        var service = new google.maps.DistanceMatrixService();
        service.getDistanceMatrix({
            origins: [source],
            destinations: [destination],
            travelMode: google.maps.TravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.METRIC,
            avoidHighways: false,
            avoidTolls: false
        }, function (response, status) {
            if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
                var distance = response.rows[0].elements[0].distance.text;
                var duration = response.rows[0].elements[0].duration.text;
                var dvDistance = document.getElementById("dvDistance");
                dvDistance.innerHTML = "";
                dvDistance.innerHTML += "<strong>" + "Distanza:" + "</strong>" + "&nbsp;&nbsp;" + distance + "<br />";
                dvDistance.innerHTML += "<strong>" + "Durata:" + "</strong>" + "&nbsp;&nbsp;" + duration;

            } else {
                alert("Impossibile trovare la distanza stradale");
            }
});
    }
</script>

it's activated by a onclick="GetRoute()" command in the request form, where I input my departure and destination in two fields. 它由请求表单中的onclick =“ GetRoute()”命令激活,我在两个字段中输入出发地和目的地。 It will then display a map with the two locations with the route informations by its side. 然后,它将在旁边显示带有两个位置的地图以及路线信息。 Now, what I want, is to activate it with asp, since I need to access my sql.So the button type will become a submit without the onclick instruction, the page will reload, I'll get the data I need questioning my database and the gmaps script will run as usual, automatically triggering that GetRoute() function. 现在,我想要的是用asp激活它,因为我需要访问我的sql。因此,按钮类型将变为没有onclick指令的提交,页面将被重新加载,我将得到我需要查询数据库的数据并且gmaps脚本将照常运行,自动触发该GetRoute()函数。 I hope I've been clear, my english is a bit rusty, I could provide the work in progress address where I'm trying to fix this bad boy. 我希望我已经很清楚了,我的英语有点生锈了,我可以提供进行中的地址来解决这个坏男孩。

I'd like the whole script to be activated automatically after the page is reloaded, without the onclick command. 我希望在重新加载页面后自动激活整个脚本,而无需使用onclick命令。 So, basically, I want to get rid of the GetRoute() command. 因此,基本上,我想摆脱GetRoute()命令。 As long as I have the data needed by the script (txtSource and txtDestination), is there a way to obtain my map and route, once the page has reloaded? 只要我拥有脚本所需的数据(txtSource和txtDestination),就可以在页面重新加载后获取我的地图和路线?

You could do this with a slight modification to your existing code, using the GetRoute function, without the onclick : 您可以使用GetRoute函数对现有代码进行一些修改,而无需onclick

In the GetRoute function, set the source and destination variables to the request values: 在GetRoute函数中,将source变量和destination变量设置为请求值:

    function GetRoute() 
    {

        source = "<%=Request("txtSource")%>"; 
        destination = "<%=Request("txtDestination")%>"; 


        if( source != "" && destination != "")
        {

            /*the rest of your code as is,except the following change:
            comment out the following two lines, as we are already setting this with the request values
            source = document.getElementById("txtSource").value;
            destination = document.getElementById("txtDestination").value;
            */


        }
     }

And in the html, for the body tag, add <body onload="GetRoute();"> so that the function will be called after all the elements are loaded. 在html中,为body标签添加<body onload="GetRoute();">以便在加载所有元素之后调用该函数。

thank you for your help. 谢谢您的帮助。 I used your suggestions and the map appears, but without the route, so maybe I did something wrong in the code. 我使用了您的建议,地图出现了,但是没有路线,所以也许我在代码中做错了。 I can confiirm that the two requests (txtsource and txtdestination) are passed correctly. 我可以确认两个请求(txtsource和txtdestination)已正确传递。

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script><script type="text/javascript">
    var source, destination;
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    google.maps.event.addDomListener(window, 'load', function () {
        new google.maps.places.SearchBox(document.getElementById('txtSource'));
        new google.maps.places.SearchBox(document.getElementById('txtDestination'));
        directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': true });
    });

    function GetRoute() 
    {
    source = "<%=Request.form("txtSource")%>"; 
    destination = "<%=Request.form("txtDestination")%>"; 

    if( source != "" && destination != "")
    {
        var rome = new google.maps.LatLng(41.918357, 12.485029);
        var mapOptions = {
            zoom: 4,
            center: rome
        };
        map = new google.maps.Map(document.getElementById('dvMap'), mapOptions);
        directionsDisplay.setMap(map);
        directionsDisplay.setPanel(document.getElementById('dvPanel'));

        //*********DIRECTIONS AND ROUTE**********************//
        /*source = document.getElementById("txtSource").value;
        destination = document.getElementById("txtDestination").value;
        */

        var request = {
            origin: source,
            destination: destination,
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });

        //*********DISTANCE AND DURATION**********************//
        var service = new google.maps.DistanceMatrixService();
        service.getDistanceMatrix({
            origins: [source],
            destinations: [destination],
            travelMode: google.maps.TravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.METRIC,
            avoidHighways: false,
            avoidTolls: false
        }, function (response, status) {
            if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
                var distance = response.rows[0].elements[0].distance.text;
                var duration = response.rows[0].elements[0].duration.text;
                var dvDistance = document.getElementById("dvDistance");
                dvDistance.innerHTML = "";
                dvDistance.innerHTML += "<strong>" + "Distanza:" + "</strong>" + "&nbsp;&nbsp;" + distance + "<br />";
                dvDistance.innerHTML += "<strong>" + "Durata:" + "</strong>" + "&nbsp;&nbsp;" + duration;

            } else {
                alert("Impossibile trovare la distanza stradale");
            }


        });
    }
    }
    </script>

Edit: I have divs in my HTML for the map and the route. 编辑:我在HTML中有用于地图和路线的div。 That doesn't appear to be the problem, since the map shows, but I have no route on it 因为地图显示了,所以这似乎不是问题,但是我没有路线

<div class="row">
<div class="col-md-6">
<div id="dvMap" style="height: 500px"></div>
</div>
<div class="col-md-6" style="height:500px; overflow-x:hidden; overflow-y:auto;">
<div id="dvPanel" ></div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM