I am planning to use Google Places API in order to create an address autoplete 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 autoplete 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?
I am planning to use Google Places API in order to create an address autoplete 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 autoplete 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?
3 Answers
Reset to default 10 +50As far as what the user types into the input box that is associated with the Autoplete
dev-guide, there isn't very much you can do to control what they type. However, when you set up the Autoplete
api-doc, you can define options that control the results that will e 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 e back in the Autoplete
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']
};
autoplete = new google.maps.places.Autoplete(input, options);
Specific to your question #2, you can restrict the results that e back in the Autoplete
to cities by setting types
to cities
as shown here:
var input = document.getElementById('searchTextField');
var options = {
types: ['(cities)'],
ponentRestrictions: {country: 'fr'}
};
autoplete = new google.maps.places.Autoplete(input, options);
Also notice that because the Autoplete
has been restricted to (cities)
, I have added a ponentRestrictions
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
toAddress
. This will allow you to associate manyAddress
records to a singleCity
record. It will also make it simple to retrieve all of theAddress
records that have been entered for anyCity
. - The relationship between
Address
andCity
says that for everyAddress
, aCity
must exist. This means that when a user enters anAddress
, you must take the following actions: 1 - Check to see if theCity
for theAddress
already exists in the database. 2 - If theCity
does exist, retrieve itsID
and use that as the foreign keyCity-ID
value when storing the newAddress
. 3 - If theCity
does not exist, a new uniqueID
must be created for theCity
and theCity
must be stored in the database. Then theID
for theCity
may be used as the foreign keyCity-ID
value when storing theAddress
. Making sure that everyAddress
has an associatedCity
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 theCity
table. Retrieving theID
for theCity
is straightforward if another user enters the sameCity
or anAddress
associated with the sameCity
is entered by a user in the future. - The relationship between
City
andAddress
says that for anyCity
there may be zero or more associatedAddress
records. This ensures that the user in your description that searches for just aCity
may store theCity
even if no follow-upAddress
searches take place. TheCity
is stored, it has anID
, and it is just waiting for any newAddress
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 -
This is more a Javascript question than Google Places.
Use a Javascript routine to validate the form, and, say in case #1, checks that all fields are filled out. On the other hand, for case #2, you could disable/remove the fields you don't want filled out, and post the request with only what you want to send.
For #3, it's just a question of caching. Use localStorage, or an array, to store the city as key, and the result as value.
Google Places will help your user autoplete their address as Sean Mickey describes:
- They start typing their street address in a single field
- Google gives them a list of possible matching addresses
- They pick one, and you get back a response like this
The id
in that response is going to be for their street address, not for the city. So it's not going to be useful the way you want. If you want to match on city, you need to store the name of the city (if you want you can create your own id table).
Think about how you want to implement location search in your database:
- Search by city (misses people who live just outside the city limits)
- Search by distance (finds everyone within a certain radius)
If you choose to search by distance, you should also be storing the Latitude and Longitude that Google Places gives you back when the user enters their address. You also need to read up on how to do spatial search in your database (especially geodist).