程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了自行车层在第二次发出并显示路线请求时消失。 Google Maps API 路线大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决自行车层在第二次发出并显示路线请求时消失。 Google Maps API 路线?

开发过程中遇到自行车层在第二次发出并显示路线请求时消失。 Google Maps API 路线的问题如何解决?下面主要结合日常开发的经验,给出你关于自行车层在第二次发出并显示路线请求时消失。 Google Maps API 路线的解决方法建议,希望对你解决自行车层在第二次发出并显示路线请求时消失。 Google Maps API 路线有所启发或帮助;

我正在使用 Google Maps API 开发自行车路线规划器。

一切都按预期工作,但只是第一次发出路线请求并显示在地图上。在随后的请求中,自行车层(在地图上用绿线显示)消失了。我希望自行车层始终可见。

我怀疑这与方向显示.setMap(map)有关,但我不确定。

我认为在重绘地图时需要使用自行车层变量,但我不知道该怎么做。

它是在以下视频的帮助下创建的:https://www.youtube.com/watch?v=BkGtNBrOhKU&t=1843s

GitHub 托管版本项目:https://gregKaighin.github.io/bedfordshire-bicycle-club/routes.html

    // Create bedforshire variable
    const bedfordshire = {
        lat: 52.02973,lng: -0.45303
    };
    // Set the map options
    const mapOtions = {
        center: bedfordshire,zoom: 10,// disable the default map UI,enable the zoom control
        DisabledefaultUI: true,zoomControl: true
    };
    // Create the map with the bicycle layer enabled
    const map = new Google.maps.Map(document.getElementByID('GoogleMap'),mapOtions);
    var bikeLayer = new Google.maps.bicyclingLayer();
    bikeLayer.setMap(map);
    // Create variables for custom legend icons 
    const icons = {
        bike@R_874_9829@ls: {
            name: "Bike lanes & @R_874_9829@ls",icon: "assets/img/icons/bike @R_874_9829@l.png",},bikeFrIEndlyroads: {
            name: "Bike-frIEndly roads",icon: "assets/img/icons/bike frIEndly road.png",};
    // Create the legend and place the icons
    const legend = document.getElementByID("legend");

    for (const key in icons) {
        const type = icons[key];
        const name = type.name;
        const icon = type.icon;
        const div = document.createElement("div");
        div.INNERHTML = '<img src="' + icon + '"> ' + name;
        legend.appendChild(div);
    }
    // Push the legend to the map
    map.controls[Google.maps.Controlposition.lefT_BottOM].push(legend);
    // Create a Directionsservice object to use the route method and get a result for the request
    var directionsservice = new Google.maps.Directionsservice();
    // Create a DirectionsRenderer object to create the route
    var directionsdisplay = new Google.maps.DirectionsRenderer();
    // display the directions on the map
    directionsdisplay.setMap(map);
    // define the calcRoute function
    function calcRoute() {
        // Create a route request
        var request = {
            origin: document.getElementByID('from').value,desTination: document.getElementByID('to').value,travelmode: Google.maps.Travelmode.bICYCliNG,unitSystem: Google.mapS.UnitSystem.IMPERIAL
        }
        // Pass the request to the .route method
        directionsservice.route(request,function (result,status) {
            if (status == Google.maps.Directionsstatus.OK) {
                // Get the route distance and time and pass to the #output div
                const output = document.querySELEctor('#output');
                output.INNERHTML = '<div class="alert-info">From: ' + document.getElementByID('from').value + '.<br />To: ' + document.getElementByID('to').value + '.<br /> Cycling distance <i class="fas fa-biking"></i> : ' + result.routes[0].legs[0].distance.text + '.<br />Duration <i class="fas fa-stopwatch"></i> : ' + result.routes[0].legs[0].duration.text + '.</div>';
                // display the route
                directionsdisplay.setDirections(result);
            } else {
                // delete the route
                directionsdisplay.setDirections({
                    routes: []
                });
                // Recenter the map on bedfordshire
                map.setCenter(bedfordshirE);
                // Show an error message if the route is not possible
                output.INNERHTML = '<div class="alert-danger"><i class="fas fa-exclamation-triangle"></i> This route is not possible on a bicycle!</div>';
            }
        });
    }
    // Create searchBox1 object for the starTing place
    var input1 = document.getElementByID('from');
    var searchBox1 = new Google.maps.places.SearchBox(input1);
    // Bias the SearchBox1 results toWARDs current map's vIEwport
    map.addListener('bounds_changed',() => {
        searchBox1.setBounds(map.getBounds());
    });
    // Create searchBox2 object for the desTination
    var input2 = document.getElementByID('to');
    var searchBox2 = new Google.maps.places.SearchBox(input2);
    // Bias the SearchBox2 results toWARDs current map's vIEwport
    map.addListener('bounds_changed',() => {
        searchBox2.setBounds(map.getBounds());
    });
    /* Form placeholder text */
    #from {
        Font-size: 0.8em;
    }

    #to {
        Font-size: 0.8em;
    }
    /* Size the map and apply Box-shadow */
    #GoogleMap {
        wIDth: 85vw;
        height: 50vh;
        margin: 12px auto;
        border: 1px solID black;
        border-radius: 2% 2% 2% 2%;
       }
    /* Style the map legend */
    #legend {
        Font-family: Arial,sans-serif;
        BACkground: #fff;
        BACkground-color: lightgray;
        padding: 4px;
        margin: 4px;
        border: 1px solID black;
        border-radius: 2% 2% 2% 2%;
        }

    #legend img {
        vertical-align: mIDdle;
    }
    /* Style the alert Box containing the route details */
    .alert-info {
        margin: auto;
        max-wIDth: fit-content;
        text-align: center;
        Font-size: 0.8em;
        BACkground-color: #efefef;
        }
    <!DOCTYPE HTML>
    <HTML lang="en-us">

    <head>
        <Meta name="vIEwport" content="initial-scale=1.0">
<form>
    <label for="from" class="control-label direction-input"></label>
        <input type="text" ID="from" placeholder="StarTing place" class="form-control">
    <label for="to" class="control-label direction-input"></label>
         <input type="text" ID="to" placeholder="DesTination" class="form-control">
</form>
     <button onclick="calcRoute();">Show Route </button>
        <div ID="map-container">
          <div ID="GoogleMap"></div>
            <div ID="legend">
           </div>
         </div>
            <div ID="output">
         </div>
        </div> 
        <!-- Google Maps API -->
        <script
            src="https://maps.GoogleAPIs.com/maps/API/Js?key=AIzaSyCZA8vB1HcWG1pqWyUyBcyuRI2VDi_fU9U&callBACk&librarIEs=places">
        </script>
        <!-- Enables drawing of the bicycle layer on the map -->
        <script src="https://polyfill.io/v3/polyfill.min.Js?features=default"></script>
        <!-- JavaScript -->
        <script src="assets/Js/routes.Js"></script>

    </body>

    </HTML>

解决方法

在显示 BICYCLE 方向时显示自行车层似乎存在竞争条件。 DirectionsRenderer 也尝试显示它。

相关问题:How to turn off the Bicycling layer after it gets turned on by the DirectionsRenderer

最简单的解决方案,因为您已经在显示 BicyclingLayer,所以阻止 DirectionsRenderer 尝试呈现它。

var directionsDisplay = new google.maps.DirectionsRenderer({
  suppressBicyclingLayer: true
});

更新的代码片段:

// Create Bedforshire variable
    const bedfordshire = {
        lat: 52.02973,lng: -0.45303
    };
    // Set the map options
    const mapOtions = {
        center: bedfordshire,zoom: 10,// Disable the default map UI,enable the zoom control
        disableDefaultUI: true,zoomControl: true
    };
    // Create the map with the bicycle layer enabled
    const map = new google.maps.Map(document.getElementById('googleMap'),mapOtions);
    const bikeLayer = new google.maps.bicyclingLayer();
    bikeLayer.setMap(map);
    // Create variables for custom legend icons 
    const icons = {
        bike@R_874_9829@ls: {
            name: "Bike lanes & @R_874_9829@ls",icon: "assets/img/icons/bike @R_874_9829@l.png",},bikeFriendlyroads: {
            name: "Bike-friendly roads",icon: "assets/img/icons/bike friendly road.png",};
    // Create the legend and place the icons
    const legend = document.getElementById("legend");

    for (const key in icons) {
        const type = icons[key];
        const name = type.name;
        const icon = type.icon;
        const div = document.createElement("div");
        div.innerHTML = '<img src="' + icon + '"> ' + name;
        legend.appendChild(div);
    }
    // Push the legend to the map
    map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(legend);
    // Create a Directionsservice object to use the route method and get a result for the request
    var directionsservice = new google.maps.Directionsservice();
    // Create a DirectionsRenderer object to create the route
    var directionsDisplay = new google.maps.DirectionsRenderer({
      suppressBicyclingLayer: true
    });
    // Display the directions on the map
    directionsDisplay.setMap(map);
    
    // Define the calcRoute function
    function calcRoute() {
        // Create a route request
        var request = {
            origin: document.getElementById('from').value,desTination: document.getElementById('to').value,travelmode: google.maps.Travelmode.bICYCLING,unitSystem: google.mapS.UnitSystem.IMPERIAL
        }
        // Pass the request to the .route method
        directionsservice.route(request,function (result,status) {
            if (status == google.maps.DirectionsStatus.OK) {
                // Get the route distance and time and pass to the #output div
                const output = document.querySELEctor('#output');
                output.innerHTML = '<div class="alert-info">From: ' + document.getElementById('from').value + '.<br />To: ' + document.getElementById('to').value + '.<br /> Cycling distance <i class="fas fa-biking"></i> : ' + result.routes[0].legs[0].distance.text + '.<br />Duration <i class="fas fa-stopwatch"></i> : ' + result.routes[0].legs[0].duration.text + '.</div>';
                // Display the route
                directionsDisplay.setDirections(result);

            } else {
                // delete the route
                directionsDisplay.setDirections({
                    routes: []
                });
                // Recenter the map on Bedfordshire
                map.setCenter(bedfordshirE);
                // Show an error message if the route is not possible
                output.innerHTML = '<div class="alert-danger"><i class="fas fa-exclamation-triangle"></i> This route is not possible on a bicycle!</div>';
            }
        });
    }
    // Create searchBox1 object for the starTing place
    var input1 = document.getElementById('from');
    var searchBox1 = new google.maps.places.SearchBox(input1);
    // Bias the SearchBox1 results toWARDs current map's viewport
    map.addListener('bounds_changed',() => {
        searchBox1.setBounds(map.getBounds());
    });
    // Create searchBox2 object for the desTination
    var input2 = document.getElementById('to');
    var searchBox2 = new google.maps.places.SearchBox(input2);
    // Bias the SearchBox2 results toWARDs current map's viewport
    map.addListener('bounds_changed',() => {
        searchBox2.setBounds(map.getBounds());
    });
/* Form placeholder text */
    #from {
        font-size: 0.8em;
    }

    #to {
        font-size: 0.8em;
    }
    /* Size the map and apply box-shadow */
    #googleMap {
        width: 85vw;
        height: 50vh;
        margin: 12px auto;
        border: 1px solid black;
        border-radius: 2% 2% 2% 2%;
       }
    /* Style the map legend */
    #legend {
        font-family: Arial,sans-serif;
        BACkground: #fff;
        BACkground-color: lightgray;
        padding: 4px;
        margin: 4px;
        border: 1px solid black;
        border-radius: 2% 2% 2% 2%;
        }

    #legend img {
        vertical-align: middle;
    }
    /* Style the alert box containing the route details */
    .alert-info {
        margin: auto;
        max-width: fit-content;
        text-align: center;
        font-size: 0.8em;
        BACkground-color: #efefef;
        }
<!DOCTYPE html>
    <html lang="en-us">

    <head>
        <meta name="viewport" content="initial-scale=1.0">
<form>
    <label for="from" class="control-label direction-input"></label>
        <input type="text" id="from" placeholder="StarTing place" class="form-control" value="Milton Keynes">
    <label for="to" class="control-label direction-input"></label>
         <input type="text" id="to" placeholder="DesTination" class="form-control" value="Newport Pagnell">
</form>
     <button onclick="calcRoute();">Show Route </button>
        <div id="map-container">
          <div id="googleMap"></div>
            <div id="legend">
           </div>
         </div>
            <div id="output">
         </div>
        </div> 
        <!-- Google Maps API -->
        <script
            src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callBACk&libraries=places">
        </script>
        <!-- Enables drawing of the bicycle layer on the map -->
        <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
        <!-- JavaScript -->
        <script src="assets/js/routes.js"></script>

    </body>

    </html>

大佬总结

以上是大佬教程为你收集整理的自行车层在第二次发出并显示路线请求时消失。 Google Maps API 路线全部内容,希望文章能够帮你解决自行车层在第二次发出并显示路线请求时消失。 Google Maps API 路线所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。