Click here to Skip to main content
15,921,884 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I had develop the web application for google map api address search
JavaScript
<script src="https://maps.googleapis.com/maps/api/js?key=apikey&libraries=places&callback=initAutocomplete" async defer></script>


This is googlemap api url

when search the address I had called initAutocomplete callback
JavaScript
window.initAutocomplete = function (searchText, compoent, name) {
    try {
        const strictBoundsInputElement = document.getElementById(
            "use-strict-bounds"
        );
        const options = {
            fields: ["formatted_address", "address_components", "geometry", "name"],
            strictBounds: false,
            types: ["premise"],
        };
 
        if (name == "Ordering")
            var autocomplete = new google.maps.places.Autocomplete($("#autocomplete")[0], { options, strictBoundsInputElement });

        autocomplete.setComponentRestrictions({ 'country': ['uk'] });
        autocomplete.setComponentRestrictions("bounds");
 
        const infowindow = new google.maps.InfoWindow();
        const infowindowContent = document.getElementById(
            "infowindow-content"
        );
        infowindow.setContent(infowindowContent);
 
        const marker = new google.maps.Marker({
 
            anchorPoint: new google.maps.Point(0, -29),
        });
        google.maps.event.addListener(autocomplete, 'place_changed', function (e) {
            infowindow.close();
            marker.setVisible(false);
 
            place = autocomplete.getPlace();
            if (!place.geometry || !place.geometry.location) {
                window.alert("No details available for input: '" + place.name + "'");
                return;
            }
 
            marker.setPosition(place.geometry.location);
            marker.setVisible(true);
 
            var street_addr = autocomplete.getPlace().formatted_address;
            var latitude = autocomplete.getPlace().geometry.location.lat();
            var langitude = autocomplete.getPlace().geometry.location.lng();
            var placeResult = autocomplete.getPlace();
            var addressComponents = placeResult.address_components;
            var street = "";
            var number = "";
            var premise = "";
            var postalcode = "";
            var administrative_area_level_1 = "";
            var administrative_area_level_2 = "";
            var locality = "";
            var postal_town = "";
            var country = "";
         
 
 
        });
    }
    catch (error) {
        console.log(error.message);
        // Call the error callback function and pass the error message
        //errorCallback(error.message);
    }
 
};

#autocomplete is textbox when internet not have i.e offline at the time if we search the address ,
https://maps.googleapis.com/maps/api/place/js/AutocompletionService.GetPredictions this api failed fetch that means
Failed to load resource: net::ERR_INTERNET_DISCONNECTED . This error find on browser console. I am unable to catch this exception on scriptside using catch block. I want to handle this exception if failed to feacth the api then i want to show user friendly message to enduser. How will handle properly and show the user fridenly message

What I have tried:

this api failed fetch that means
Failed to load resource: net::ERR_INTERNET_DISCONNECTED . This error find on browser console. I am unable to catch this exception on scriptside using catch block. I want to handle this exception if failed to feacth the api then i want to show user friendly message to enduser. How will handle properly and show the user fridenly message.Please helpme. Thanks
Posted
Comments
Krishna Veni 13-May-24 12:37pm    
please reply how to handle

1 solution

I'm pretty sure that you are going to have to add the try/catch inside the place_changed event listener. Your existing try/catch is too broad. Basically, you have the event handler around adding the event listener, and not around what happens inside the event function.

Now, the reason you are unable to handle this particular error is because you are listening for the wrong thing. What you need to catch is the window.offline[^] event, which is raised when your application goes offline. (There is a corresponding online event when the application comes back online). Your code will look something like this:
JavaScript
window.addEventListener("offline", (event) => {
  applicationOffline();
});
 
Share this answer
 
v3
Comments
Krishna Veni 14-May-24 7:49am    
'place_changed' event can not execute if application in offline thats why try and catch not used inside 'place_changed' event . Predications can not get from this api https://maps.googleapis.com/maps/api/place/js/AutocompletionService.GetPredictions if application offline. Please helpme. Thanks
Pete O'Hanlon 14-May-24 8:24am    
My apologies, the reason you aren't able to handle this here is because you are listening for the wrong thing. What you need to do is add an event handler for the offline event on the window.
Krishna Veni 14-May-24 12:08pm    
Thanks for offline event but my intention is i dont want to check application offline/online. mostly api failed to fetch the api if application offline but i want to show custom message if failed fetch the api based on that api failed response but i dont want to check application offline/online event. If there is another solution,please help me. thanks
Krishna Veni 14-May-24 12:33pm    
https://maps.googleapis.com/maps/api/place/js/AutocompletionService.GetPredictions

This api failed to cal that is ERR_INTERNET_DISCONNECTED errorcode. i want to handle this. using catch not possible. Any
google map api error event handler is there ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900