简体   繁体   English

Google Maps是否加载?

[英]Google Maps load or not check?

I have developed a Web application in which i use Goole Maps in my web application. 我已经开发了一个Web应用程序,在其中可以在Web应用程序中使用Goole Maps。

HTML: HTML:

<div id="showMap">
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="map"></div>
</div>
<div id="errorMap">
<h1>No Map available due to Internet connectivity problems.</h1>
</div>

JS: JS:

function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 31.601043, lng: 74.169527},
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var input = document.getElementById('pac-input');
    var searchBox = new google.maps.places.SearchBox(input);
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    // Bias the SearchBox results towards current map's viewport.
    map.addListener('bounds_changed', function () {
        searchBox.setBounds(map.getBounds());
    });

    // Listen for the event fired when the user selects a prediction and retrieve
    // more details for that place.
    searchBox.addListener('places_changed', function () {
        var places = searchBox.getPlaces();

        if (places.length == 0) {
            return;
        }

        // Clear out the old markers.
        markers.forEach(function (marker) {
            marker.setMap(null);
        });
        markers = [];

        // For each place, get the icon, name and location.
        var bounds = new google.maps.LatLngBounds();
        places.forEach(function(place) {
            var icon = {
                url: place.icon,
                size: new google.maps.Size(71, 71),
                origin: new google.maps.Point(0, 0),
                anchor: new google.maps.Point(17, 34),
                scaledSize: new google.maps.Size(25, 25)
            };

            // Create a marker for each place.
            markers.push(new google.maps.Marker({
                map: map,
                icon: icon,
                title: place.name,
                position: place.geometry.location
            }));

            if (place.geometry.viewport) {
                // Only geocodes have viewport.
                bounds.union(place.geometry.viewport);
            } else {
                bounds.extend(place.geometry.location);
            }
        });
        map.fitBounds(bounds);
    });

    loadMarkersDynamically(); // load markers function
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDwEvCr3alGTKBFt23yk4WVq5jg34I7CJs&libraries=places&callback=initMap" async defer></script>

Everything is working fine. 一切正常。 But I have one problem when there is no internet connection then error come and map do not displayed. 但是,当没有互联网连接时,我遇到一个问题,那就是错误来了并且地图不显示。 I want that if internet is not available then I want to hide showMap div and show errorMap div. 我希望如果互联网不可用,那么我想隐藏showMap div并显示errorMap div。

I have tried so many solutions: This , This and This from Stack Overflow but I am not able to solve my problem. 我尝试了很多解决方案:Stack Overflow的ThisThisThis ,但是我无法解决我的问题。

Maybe I am not calling function at right place that is provides in above answers, please tell me how to fix it. 也许我没有在上述答案中提供的正确位置调用函数,请告诉我如何修复它。

You could use HTML5 to check, using the navigator.onLine 您可以使用navigator.onLine使用HTML5进行检查

Like so: 像这样:

if(navigator.onLine)
{
   //Continue with your code.
}
else
{
   //Show your error div.
}

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

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