Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to completely remove GoogleMaps Autocomplete?

I use GoogleMap v3 AutoComplete and I need to completely remove it and unbind all event listeners. My code for initializing and binding to events looks like the following:

var autocomplete = new google.maps.places.Autocomplete($("input").get(0), {
   types: ["geocode"]
});

google.maps.event.addListener(autocomplete, 'place_changed', function () {
  // handle events
});

I don't find official way to correctly remove autocomplete and unbind all events. Please point me the right way.

Thanks.

like image 230
Erik Avatar asked Feb 10 '15 21:02

Erik


People also ask

How do I restrict Google Maps autocomplete to certain cities?

It is currently not possible to restrict results to a specific locality. You can use bounds as you have done so above to bias results towards, but not restricted to places contained within the specified bounds. If you believe restriction by locality would be a useful feature please file a Places API - Feature Request.

Can we remove powered by Google in autocomplete?

Go to look and feel-> customize tab-> Google branding-> Disable Google branding. Click on save and get code. Add this new code to your website. Now you can see that “powered by google custom search” branding has been removed.

How do I get rid of markers on Google Maps?

Click the marker to reveal the editing options. Click the "Delete" link in the lower-left corner of the dialog box.


1 Answers

For the unbinding of events use google.maps.event.clearInstanceListeners.

For removing of the autocomplete-functionality there is no implemented method. You may create a clone of the input before you create the Automplete and when you want to remove the autocomplete-functionality replace the current input with the clone.

//--------------------------------------------------------------
      //this overides the built-in Autocomplete and adds a remove-listener
      //execute it once when the API has been loaded
     (function(ac) {
         google.maps.places.Autocomplete = function(node, opts) {
           var clone = node.cloneNode(true),
             pac = new ac(node, opts);

           google.maps.event
             .addListener(pac,
               'remove',
               function(restore) {
                   google.maps.event.clearInstanceListeners(pac);
                   google.maps.event.trigger(node,'blur');
                   google.maps.event.clearInstanceListeners(node);
                 if (restore===true) {
                   node.parentNode.replaceChild(clone, node);
                 } else {
                   node.parentNode.removeChild(node)
                 }
               });
           return pac;

         }
       }
       (google.maps.places.Autocomplete));
//-------------------------------------------------------------------------- 

     function initialize() {

       autocomplete = new google.maps.places
         .Autocomplete(document.getElementsByTagName('INPUT')[0], {
           types: ["geocode"]
         });
     }

     google.maps.event.addDomListener(window, 'load', initialize);
<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places&.js"></script>
<input/>
<span>
  <input  type="button"  value="remove input" 
          onclick="google.maps.event.trigger(window.autocomplete,'remove');
                   this.parentNode.parentNode.removeChild(this.parentNode);"/>
  <input  type="button"  value="remove autocomplete-functionality" 
          onclick="google.maps.event.trigger(window.autocomplete,'remove',true);
                   this.parentNode.parentNode.removeChild(this.parentNode);"/>
<span>

The script adds a remove-listener to Automplete's. The listener accepts a single argument. Set it to true when you only want to remove the autocomplete-functionality. Otherwise the input will be removed completely.

like image 186
Dr.Molle Avatar answered Nov 10 '22 12:11

Dr.Molle