ubilabs / geocomplete

jQuery Geocoding and Places Autocomplete Plugin
http://ubilabs.github.com/geocomplete/
MIT License
1.22k stars 406 forks source link

Blur option not working properly #119

Open gschoppe opened 10 years ago

gschoppe commented 10 years ago

I am using the following code to initialize my geocomplete:

var lastValue = $('#address').val();
$('#address').geocomplete({
    map: '#map',
    details: '#addressdetails',
    mapOptions: {draggable: false, zoomControl: false, streetViewControl: false, scrollwheel: false, disableDoubleClickZoom: true},
    country: 'us',
    blur: true
}).on('change keyup paste mouseup', function() {
   if ($(this).val() != lastValue) {
        lastValue = $(this).val();
        $('#lat').val("");
        $('#lng').val("");
    }
}).bind("geocode:result", function(){
    lastValue = $(this).val();
});

the expected result is that once I leave the form, triggering blur, it is geocoded to my preferences. The actual result is that the "auto-geocoding" happens when I choose an address from the dropdown, or whenever I leave the textfield, and overrides the existing, good geocoding result with one selected without regards to my country limitation.

For example, if I enter "18", the first dropdown choice is 1838 Spaulding Rd, St Johnsbury, VT 05819. However, when I click it, My address changes to 18, Denmark (despite the country bound i set), and that address is shown on the map.

This same incorrect geocoding happens if I click outside the box or tab away after entering data.

seanbfuller commented 10 years ago

I am also seeing this. I was able to track it down to the this.find() call on line 195 (inside the bind of the blur event). I replaced that call with code from the placeChanged method (around line 456), that adds the selectFirstResult() return as a parameter for find. The end result looked like this:

      if (this.options.blur === true){
        this.$input.blur($.proxy(function(){
          //this.find();
          var autoSelection = this.selectFirstResult();
          this.find(autoSelection);
        }, this));
      }

This seems to be working for my purposes, but more testing may be needed.

cuchac commented 9 years ago

This solution works for me as well. Thanks!

cmwelsh commented 9 years ago

I've been seeing this too. Thanks for the tip.

cmwelsh commented 9 years ago

This doesn't seem applicable to the current version - there's no difference between this hack and setting blur to false. I definitely experience this issue though.

It's really frustrating because it grabs results from other countries and I want to limit to USA results...

cuchac commented 9 years ago

To my knowledge there IS difference. If 'blur' is false, the 'blur' event is not bound on the input filed.

sidazhou commented 9 years ago

I've used the snippet from seanbfuller commented on Apr 28, 2014 and I've set blur: true, geocodeAfterResult: false, restoreValueAfterBlur: true So far it yields results I wanted, I've removed the find button, and I'm trying to restrict the input to always have a valid city. So when user blur, then auto-select the first selection.

95 claims to fix this, but I get the problem still.