0

I have developed a Web application in which i use Goole Maps in my web application.

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:

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.

I have tried so many solutions: This, This and This from Stack Overflow but I am not able to solve my problem.

Maybe I am not calling function at right place that is provides in above answers, please tell me how to fix it.

Community
  • 1
  • 1
Ahmad
  • 1,462
  • 5
  • 17
  • 40
  • 1
    Is it for an app in phonegap or an internal web application? I don't understand how people should access your site without internet connection... – Jesper Højer May 27 '16 at 10:06
  • basically it is a real estate application, i will deliver it with WAR file, it is not a website, maps is optional, and if internet is not available so i have to hide map div and show error message...it everytime run on local server of client also. – Ahmad May 27 '16 at 10:12
  • please tell me how to fix it – Ahmad May 27 '16 at 10:18
  • Pro-tips for asking questions: don't try to rush readers by mentioning your deadline; don't explicitly ask for readers to fix your code for you; and don't make special requests that people should (or should not) vote on questions on a certain way. – halfer May 30 '16 at 09:22
  • ok understand your point, can you help me ? because still my problem is not solved – Ahmad May 30 '16 at 09:43

1 Answers1

0

You could use HTML5 to check, using the navigator.onLine

Like so:

if(navigator.onLine)
{
   //Continue with your code.
}
else
{
   //Show your error div.
}
Jesper Højer
  • 2,942
  • 1
  • 16
  • 26
  • i tried it but every time when internet is available and when internet is not available, both times if is true and its part executed – Ahmad May 27 '16 at 10:23
  • You could try this, but not sure it it's supported in all browsers. if(navigator.onLine) - check updated solution. – Jesper Højer May 27 '16 at 10:27
  • sorry i checked it again but both time if part is executed. – Ahmad May 27 '16 at 11:01