As far as what the user types into the input box that is associated with the Autocomplete
dev-guide, there isn't very much you can do to control what they type. However, when you set up the Autocomplete
api-doc, you can define options that control the results that will come back. The key for you will be setting up the types
option correctly.
Specific to your question #1, you can restrict the results that will come back in the Autocomplete
to addresses by setting types
to geocode
as shown in this example:
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-33.8902, 151.1759),
new google.maps.LatLng(-33.8474, 151.2631));
var input = document.getElementById('searchTextField');
var options = {
bounds: defaultBounds,
types: ['geocode']
};
autocomplete = new google.maps.places.Autocomplete(input, options);
Specific to your question #2, you can restrict the results that come back in the Autocomplete
to cities by setting types
to cities
as shown here:
var input = document.getElementById('searchTextField');
var options = {
types: ['(cities)'],
componentRestrictions: {country: 'fr'}
};
autocomplete = new google.maps.places.Autocomplete(input, options);
Also notice that because the Autocomplete
has been restricted to (cities)
, I have added a componentRestrictions
specifier to set the country within which to search for cities (in this case, France) and removed the bounds
specifier.
Specific to your question #3, you can create two tables, one to store City data, the other to store Address data, as shown in the following UML diagram:
Based on the description in your question, there are some key aspects of this design:
- There is a one-to-many relationship from
City
to Address
. This will allow you to associate many Address
records to a single City
record. It will also make it simple to retrieve all of the Address
records that have been entered for any City
.
- The relationship between
Address
and City
says that for every Address
, a City
must exist. This means that when a user enters an Address
, you must take the following actions: 1 - Check to see if the City
for the Address
already exists in the database. 2 - If the City
does exist, retrieve its ID
and use that as the foreign key City-ID
value when storing the new Address
. 3 - If the City
does not exist, a new unique ID
must be created for the City
and the City
must be stored in the database. Then the ID
for the City
may be used as the foreign key City-ID
value when storing the Address
. Making sure that every Address
has an associated City
answers one of the questions you ask as part of your question #3: How can I find the result of the first user: "MyCity, MyStreet 12" using "MyCity" key? Because when you stored the "MyCity, MyStreet 12" Adress
record, you made sure a "MyCity" record exists in the City
table. Retrieving the ID
for the City
is straightforward if another user enters the same City
or an Address
associated with the same City
is entered by a user in the future.
- The relationship between
City
and Address
says that for any City
there may be zero or more associated Address
records. This ensures that the user in your description that searches for just a City
may store the City
even if no follow-up Address
searches take place. The City
is stored, it has an ID
, and it is just waiting for any new Address
records that may be added later.
Finally, you asked one more question as part of question #3: how can I check if the fully address belong to the city using the ids only? Being able to answer this question is why there is a foreign key City-ID
that is part of every Address
record. It clearly defines the City
that is associated with any Address
. So if you have the ID
for a City
and the ID
for an Address
, the simplest way to determine if they are a match is: 1 - Retrieve the Address
from the database using the Address
ID
. 2 - Compare the City-ID
value that is part of the Address
that was just retrieved from the database with the ID
for the City
you started with; if they match, you know the Address
is associated with the City
and if they don't match, you can be sure there is no relationship between that Address
and that City
.
I'm not entirely sure what you are trying to achieve with the addresses and the cities, but I've tried to give you a solid solution that covers the things you describe in your question. I included a great deal of detail so that all of your points are addressed and in the hope that it will make my description clear and easy to understand. I hope this helps you -
Jan 2021: autocomplete="off"
does work as expected now (tested on Chrome 88 macOS).
For this to work be sure to have your input tag within a Form tag
Sept 2020: autocomplete="chrome-off"
disables Chrome autofill.
Original answer, 2015:
For new Chrome versions you can just put autocomplete="new-password"
in your password field and that's it. I've checked it, works fine.
Got that tip from Chrome developer in this discussion:
https://bugs.chromium.org/p/chromium/issues/detail?id=370363#c7
P.S. Note that Chrome will attempt to infer autofill behavior from name, id and any text content it can get surrounding the field including labels and arbitrary text nodes. If there is a autocomplete token like street-address
in context, Chrome will autofill that as such. The heuristic can be quite confusing as it sometimes only trigger if there are additional fields in the form, or not if there are too few fields in the form. Also note that autocomplete="no"
will appear to work but autocomplete="off"
will not for historical reasons. autocomplete="no"
is you telling the browser that this field should be auto completed as a field called "no"
. If you generate unique random autocomplete
names you disable auto complete.
If your users have visited bad forms their autofill information may be corrupt. Having them manually go in and fix their autofill information in Chrome may be a necessary action from them to take.
Best Answer
But it won't show it when you initially enter the address
...because it's not available when you enter the address. The details for a prediction will be requested when you select an item, not when you type something. So the answer is simple: it's not possible.
What you can do: create your own autocomplete and populate it with data.
You'll first need to request the predictions via the autocompleteService and then request the details via PlacesService.getDetails()