Google Places API - 使用城市查找地址 [英] Google Places API - find an address using the city

查看:94
本文介绍了Google Places API - 使用城市查找地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用Google Places API来在我的网站中创建一个地址自动完成功能。我的问题是关于我的要求是否可以使用Google地方信息。



我有一个发布请求页面,用户发布地址(城市,街道和门牌号码) 。我也有一个搜索页面,用户可以根据城市搜索帖子。这里用户只能插入一个城市名称,没有街道和门牌号码。



我的问题:


  1. 我可以强制用户插入城市,街道和门牌号码(仅插入城市或城市和街道,例如提醒无效输入,否则自动填充将只返回城市,街道和门牌号码)?


  2. 我可以强制用户只插入城市(没有街道和门牌号码)吗?


  3. 假设用户使用MyCity,MyStreet 12发布地址。在应用程序的背景下,我得到了这个特定位置的ID并将其存储。另一位用户正在搜索帖子并插入MyCity。在后台我得到了MyCity的具体ID,并使用此ID来搜索我的数据库。我怎样才能找到第一个用户的结果:MyCity,MyStreet 12使用MyCity键?

    换句话说,假设我有一个位置ID代表一个城市和其他位置ID代表完全地址(城市,街道,门牌号码),我怎样才能使用ID检查完整地址是否属于城市?



Autocomplete dev-guide ,您可以做的不多控制他们输入的内容。但是,当您设置 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 ,您可以限制来自通过将类型设置为城市,返回 Autocomplete 如下所示:

  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:

  1. 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)?

  2. Can I force the user insert city only (without street and house number)?

  3. 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 the Autocompleteapi-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 -

这篇关于Google Places API - 使用城市查找地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆