I am planning to use Google Places API in order to create an address autocomplete in my web site. My question is about whether my requirements are possible using Google Places.
I have a “Post Request” page where the user posts an address (city, street and house number). I also have a “Search” page where the user can search posts according to the city. Here the user only inserts a city name, without a street and house number.
My questions:
-
Can I force the user to insert a city, street and house number (inserts of only city or only city and street for example will alert invalid input, or the autocomplete will return only results of city, street and house number)?
-
Can I force the user insert city only (without street and house number)?
-
Assuming the user posts an address with “MyCity, MyStreet 12”. In the background of the application I get an id of this specific location and store it. Another user is searching for posts and inserts “MyCity”. In the background I get the specific id of “MyCity” and use this id in order to search in my db. How can I find the result of the first user: “MyCity, MyStreet 12” using “MyCity” key?
In other words, assume I have a location id that represents a city and other location id that represents fully address (city, street, house number), how can I check if the fully address belong to the city using the ids only?
As far as what the user types into the input box that is associated with the
Autocompletedev-guide, there isn’t very much you can do to control what they type. However, when you set up theAutocompleteapi-doc, you can define options that control the results that will come back. The key for you will be setting up thetypesoption correctly.Specific to your question #1, you can restrict the results that will come back in the
Autocompleteto addresses by settingtypestogeocodeas shown in this example:Specific to your question #2, you can restrict the results that come back in the
Autocompleteto cities by settingtypestocitiesas shown here:Also notice that because the
Autocompletehas been restricted to(cities), I have added acomponentRestrictionsspecifier to set the country within which to search for cities (in this case, France) and removed theboundsspecifier.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:
CitytoAddress. This will allow you to associate manyAddressrecords to a singleCityrecord. It will also make it simple to retrieve all of theAddressrecords that have been entered for anyCity.AddressandCitysays that for everyAddress, aCitymust exist. This means that when a user enters anAddress, you must take the following actions: 1 – Check to see if theCityfor theAddressalready exists in the database. 2 – If theCitydoes exist, retrieve itsIDand use that as the foreign keyCity-IDvalue when storing the newAddress. 3 – If theCitydoes not exist, a new uniqueIDmust be created for theCityand theCitymust be stored in the database. Then theIDfor theCitymay be used as the foreign keyCity-IDvalue when storing theAddress. Making sure that everyAddresshas an associatedCityanswers 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”Adressrecord, you made sure a “MyCity” record exists in theCitytable. Retrieving theIDfor theCityis straightforward if another user enters the sameCityor anAddressassociated with the sameCityis entered by a user in the future.CityandAddresssays that for anyCitythere may be zero or more associatedAddressrecords. This ensures that the user in your description that searches for just aCitymay store theCityeven if no follow-upAddresssearches take place. TheCityis stored, it has anID, and it is just waiting for any newAddressrecords 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-IDthat is part of everyAddressrecord. It clearly defines theCitythat is associated with anyAddress. So if you have theIDfor aCityand theIDfor anAddress, the simplest way to determine if they are a match is: 1 – Retrieve theAddressfrom the database using theAddressID. 2 – Compare theCity-IDvalue that is part of theAddressthat was just retrieved from the database with theIDfor theCityyou started with; if they match, you know theAddressis associated with theCityand if they don’t match, you can be sure there is no relationship between thatAddressand thatCity.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 –