In my website I have used google map to calculate distace between two places. currently that script runs on button click. I want it to run on dropdown selection. In dropdown selected value gets copied in secondNumber textbox My current code is something like this.

At the end I want to multiply textbox1 with textbox2 but before that some value needs to be put in textbox1 & it is possible only if GetRoute functions calls on dropdown selection.

<asp:DropDownList ID="selectVehicle" runat="server" CssClass="inners" AutoPostBack="true">
                            <asp:listitem>-- Select Vehicle --</asp:listitem>
                            <asp:listitem Value="20">Tata Ace</asp:listitem>
                            <asp:listitem Value="35">Tata 407</asp:listitem>
                        </asp:DropDownList>

<asp:TextBox ID="firstNumber" runat="server" style="display:none"></asp:TextBox><br /><br />
<asp:TextBox ID="secondNumber" runat="server" style="display:none"></asp:TextBox>

<asp:Button ID="btnShow" runat="server" Text="Click to Proceed" CssClass="book" OnClientClick="GetRoute();" />

Map 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() {
            debugger;
            var mumbai = new google.maps.LatLng(18.9750, 72.8258);
            var mapOptions = {
                zoom: 7,
                center: mumbai
            };
            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");
                    var dvDuration = document.getElementById("dvDuration")
                    dvDistance.innerHTML = "";
                    dvDistance.innerHTML = distance ;
                    dvDuration.innerHTML = duration;

                } else {
                    alert("Unable to find the distance via road.");
                }
            });
        }
    </script>

Here all working fine for me I just want GetRoute() function to be called on dropdown selection instead og button click...

Related posts

Recent Viewed