Google Places API - 使用城市查找地址 [英] Google Places API - find an address using the city
问题描述
我打算使用Google Places API来在我的网站中创建一个地址自动完成功能。我的问题是关于我的要求是否可以使用Google地方信息。
我有一个发布请求页面,用户发布地址(城市,街道和门牌号码) 。我也有一个搜索页面,用户可以根据城市搜索帖子。这里用户只能插入一个城市名称,没有街道和门牌号码。
我的问题:
-
我可以强制用户插入城市,街道和门牌号码(仅插入城市或城市和街道,例如提醒无效输入,否则自动填充将只返回城市,街道和门牌号码)?
-
-
我可以强制用户只插入城市(没有街道和门牌号码)吗?
-
假设用户使用MyCity,MyStreet 12发布地址。在应用程序的背景下,我得到了这个特定位置的ID并将其存储。另一位用户正在搜索帖子并插入MyCity。在后台我得到了MyCity的具体ID,并使用此ID来搜索我的数据库。我怎样才能找到第一个用户的结果:MyCity,MyStreet 12使用MyCity键?
换句话说,假设我有一个位置ID代表一个城市和其他位置ID代表完全地址(城市,街道,门牌号码),我怎样才能使用ID检查完整地址是否属于城市?
Autocomplete
api-doc ,您可以定义控制将返回的结果的选项。您的关键是正确设置类型
选项。 针对您的问题#1 ,您可以通过将类型
设置为来限制将返回到 Autocomplete
code> geocode ,如下例所示:
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 = {
界限:defaultBounds,
类型:['geocode']
};
autocomplete = new google.maps.places.Autocomplete(input,options);
针对您的问题#2 ,您可以限制来自通过将类型
设置为城市
,返回
var input = document.getElementById('searchTextField');
var options = {
types:['(cities)'],
componentRestrictions:{country:'fr'}
};
autocomplete = new google.maps.places.Autocomplete(input,options);
另请注意,由于 Autocomplete
已限制为(城市)
,我添加了一个 componentRestrictions
说明符来设置搜索城市的国家这种情况下,法国),并删除边界
说明符。
具体到您的问题#3 ,您可以创建两个表,一个用于存储城市数据,另一个用于存储地址数据,如以下UML图所示:
$ b
基于你的问题中的描述,这个设计有一些关键方面:
城市
到地址
的关系。这将允许您将许多地址
记录关联到单个城市
记录。它还可以简化检索为城市
输入的所有地址
记录。
地址
和城市
之间的关系表示对于每个 地址
,a 城市
必须存在。这意味着当用户输入地址
时,您必须采取以下措施:1 - 检查城市
在数据库中已存在 Address
>。 2 - 如果城市
不存在,请检索它的 ID
并将其作为外部在存储新的地址
时键入 City-ID
的值。 3 - 如果 City
不存在,则必须创建一个新的唯一 ID
城市
和城市
必须存储在数据库中。然后, City
的 ID
可以用作外键 City-ID <当存储
地址
时,code>值。确保每个地址
都有一个关联的城市
可以回答您在问题#3中提出的问题之一: 如何使用MyCity键查找第一个用户的结果:MyCity,MyStreet 12?因为当您存储MyCity,MyStreet 12 Adress
记录,您确保
City
表中存在MyCity记录。如果另一个用户输入相同的 City $>,那么检索
与同一个 City
的 ID
很简单c $ c>或$ 城市
相关联,这是将来用户输入的。
City
和 Address
之间的关系表明,对于任何城市
可能有零个或多个关联的地址
记录。这可以确保在描述中只搜索 City
的用户可以存储 City
,即使没有后续操作地址
搜索发生。 城市
被存储,它有一个 ID
,它只是等待任何新的
最后,您还问了另一个问题作为问题# 3:我如何使用ID查看完整地址是否属于城市?能够回答这个问题是为什么有一个外键 City-ID code>是每个
地址
记录的一部分。它清楚地定义了与任何地址
相关联的城市
。因此,如果您的城市
和 ID
对于地址
,确定它们是否匹配的最简单方法是:1 - 从中检索地址
数据库使用地址
ID
。 2 - 将刚刚从数据库中检索到的地址
的一部分 City-ID
值与 ID
为您开始的城市
;如果它们匹配,则知道 Address
与 City
相关联,如果它们不匹配,则可以确定地址
和城市
之间没有关系。
我并不完全确定你在地址和城市中想要达到的目标,但我试图给你一个可靠的解决方案,涵盖你在问题中描述的内容。我包括了很多细节,以便您的所有观点都得到解决,并希望它能使我的描述清晰易懂。我希望这可以帮助你 -
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 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
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 -
这篇关于Google Places API - 使用城市查找地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!