1

I have a Google Maps search box using autocomplete (as the user inputs text in the field, a dropdown of address suggestions appears).

I want to make sure that when a user submits the form, the text inside the field is an actual Autocomplete suggestion.

My goal is to ensure that the text entered is an actual adress, rather than a random string.

user2056289
  • 163
  • 1
  • 1
  • 7
  • Sounds like you want address validation prior to submitting to google maps for "address location". Is that right? You want to know the address is good before asking Googlemaps to locate it for you? – Jeffrey Aug 27 '13 at 04:11
  • Yes, that is basically it. But I am not sure there is a "universal" address format used by google maps – user2056289 Aug 28 '13 at 01:27
  • If you mean a universal address format across the globe, there isn't one. Some countries use similar methods for addressing but there's not a standard. Also, Googlemaps doesn't maintain a list of valid addresses, thus their autocomplete isn't going to contain only addresses that are real. It's beyond the scope of googlemaps right now. If you first validate the address and then submit it to googlemaps to show where it is you'll have much better results. Does that help? – Jeffrey Aug 28 '13 at 18:09
  • I am not necessarily looking for a universal address format. Since my website relies mostly on Googlemaps, I at least want to make sure that the address entered is one suggested by Googlemaps. In order to minimize errors on the subsequent page, which displays the address on a map. So you would suggest validating the address (How?) and then submit it to Googlemaps? Thank you for your help. – user2056289 Aug 28 '13 at 22:00
  • Well, a google search for "address validation" will show you a number of results. Many of them I've tried myself. – Jeffrey Aug 29 '13 at 03:35
  • So you're saying it's impossible to figure out if the user has selected a suggestion given by the Googlemaps autocomplete? – user2056289 Aug 29 '13 at 17:03
  • I don't understand your latest question? If the user selects a suggestion given by the googlemaps autocomplete, it will then show that result on a map, right. If you log the autocomplete results and then compare them to the output of the map lookup, you should be able to see if the output was one of the suggestions. – Jeffrey Aug 29 '13 at 17:31
  • Let me rephrase my problem. How do I prevent someone from entering gibberish and looking it up? For example "123, fskdjhre street". – user2056289 Aug 29 '13 at 19:48
  • First thing to do is to define gibberish. Once you have it clearly defined, you should be able to write a REGEX or other function that will detect it. (incidentally, once you can clearly define what is/isn't gibberish, please share what you learn. It's an unsolved problem). Is 123 jkb Provo UT gibberish? How about 1234 mlk dr baxley, ga? Or 123 poopoo pl Kailua, HI? It's hard to remove the gibberish without discarding some good data too. Here is one attempt: http://stackoverflow.com/questions/10211028/javascript-script-to-find-gibberish-words-in-form-inputs – Jeffrey Aug 30 '13 at 00:35
  • I appreciate your help and guidance and I understand the difficulty with defining a valid address. This is why I am trying to get around the problem by accessing the list of suggestions given by the autocomplete form. Ideally, there would be a way to access the Googlemaps autocomplete script and access the (5) addresses that are being suggested. We log them as the user is typing in the address, and once the form is submitted, we compare the user entry to the list of suggestions. Is it any clearer? – user2056289 Aug 30 '13 at 02:38

2 Answers2

1

I had a similar issue (that anything in the given field should be valid Google Maps address), and built a solution inspired from this one, that may help you. The idea is :

  • when the user does something else than click on a Google Maps suggestion
  • trigger the event to move focus on the first suggestion
  • trigger the event to select it

I copy the result for my own code (with Jquery) :

    //// Ensuring that only Google Maps adresses are inputted
    function selectFirstAddress (input) {
        google.maps.event.trigger(input, 'keydown', {keyCode:40});
        google.maps.event.trigger(input, 'keydown', {keyCode:13});
    }

    // Select first address on focusout
    $('#content').on('focusout', 'input#xxx__zzz', function() {
        selectFirstAddress(this);
    });

    // Select first address on enter in input
    $('#content').on('keydown', 'input#xxx__zzz', function(e) {
        if (e.keyCode == 13) {
            selectFirstAddress(this);
        }
    });

For me it works, I hope it helps !

Community
  • 1
  • 1
PolRaguénès
  • 160
  • 3
  • 12
0

The following link describes an example in which you can access autocomplete suggestions: https://developers.google.com/maps/documentation/javascript/examples/places-queryprediction

user2056289
  • 163
  • 1
  • 1
  • 7