使用Google api匹配城市和州
Matching city and state using Google API's?
我需要使用Google API检查给定的城市和州的组合是否正确。
Ways I found so far
1。USING GeoCoding API:
我们可以调用zipCode,它将返回一个JSON。有一个描述格式化地址的字段,其中包含城市、州、邮政编码、地区等。
http://maps.googleapis.com/maps/api/geocode/json?address=ZIPCODE&sensor=true
2。ZIPtastic API
与上面的类似,但它返回的信息较少。
但现在我想做没有ZIPCode。[我。e]城市和国家是给定的,我需要检查给定的组合是否正确使用API。有什么办法吗?请提一些建议。
编辑:我需要检查给定的城市,州,国家,邮政编码是否正确。
http://maps.googleapis.com/maps/api/geocode/json?address=NY&address=10001&sensor=true&components=country:US
上面的返回与
相同的结果http://maps.googleapis.com/maps/api/geocode/json?address=NY&address=10001&sensor=true&address=albanycomponents=country:US
邮编是10001,州是纽约,城市是奥尔巴尼,国家是美国。为什么它返回相同的结果。我得到正确的结果或做什么?
您可以在GeoCoding API的address
参数中使用您想要的内容,例如:
http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=true
这里我得到了匹配地址Paris
的结果:法国的Paris,德克萨斯州的Paris等。
因此,您可以使用它来输入您的城市并检查状态是否有效。为此,也将搜索限制在美国,使用以下补语:components=country:US
示例:http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=true&components=country:US
{
"results" : [
{
"address_components" : [
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Comté de Lamar",
"short_name" : "Comté de Lamar",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Texas",
"short_name" : "TX",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "États-Unis",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Paris, Texas, États-Unis",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 33.7383781,
"lng" : -95.435455
},
"southwest" : {
"lat" : 33.6118529,
"lng" : -95.62792789999999
}
},
"location" : {
"lat" : 33.6609389,
"lng" : -95.55551299999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 33.7383781,
"lng" : -95.435455
},
"southwest" : {
"lat" : 33.6118529,
"lng" : -95.62792789999999
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Comté de Henry",
"short_name" : "Comté de Henry",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Tennessee",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "États-Unis",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "38242",
"short_name" : "38242",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Paris, Tennessee 38242, États-Unis",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 36.3291321,
"lng" : -88.2650759
},
"southwest" : {
"lat" : 36.266,
"lng" : -88.36711489999999
}
},
"location" : {
"lat" : 36.3020023,
"lng" : -88.32671069999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 36.3291321,
"lng" : -88.2650759
},
"southwest" : {
"lat" : 36.266,
"lng" : -88.36711489999999
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Comté d'Edgar",
"short_name" : "Comté d'Edgar",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Illinois",
"short_name" : "IL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "États-Unis",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "61944",
"short_name" : "61944",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Paris, Illinois 61944, États-Unis",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 39.6485756,
"lng" : -87.6505408
},
"southwest" : {
"lat" : 39.581415,
"lng" : -87.721046
}
},
"location" : {
"lat" : 39.611146,
"lng" : -87.6961374
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 39.6485756,
"lng" : -87.6505408
},
"southwest" : {
"lat" : 39.581415,
"lng" : -87.721046
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Comté de Bourbon",
"short_name" : "Comté de Bourbon",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Kentucky",
"short_name" : "KY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "États-Unis",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "40361",
"short_name" : "40361",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Paris, Kentucky 40361, États-Unis",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 38.238271,
"lng" : -84.232089
},
"southwest" : {
"lat" : 38.164922,
"lng" : -84.3073259
}
},
"location" : {
"lat" : 38.2097987,
"lng" : -84.2529869
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 38.238271,
"lng" : -84.232089
},
"southwest" : {
"lat" : 38.164922,
"lng" : -84.3073259
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Comté d'Oxford",
"short_name" : "Comté d'Oxford",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Maine",
"short_name" : "ME",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "États-Unis",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Paris, Maine, États-Unis",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 44.31228309999999,
"lng" : -70.4148551
},
"southwest" : {
"lat" : 44.1753699,
"lng" : -70.56617199999999
}
},
"location" : {
"lat" : 44.2597917,
"lng" : -70.5006152
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 44.31228309999999,
"lng" : -70.4148551
},
"southwest" : {
"lat" : 44.1753699,
"lng" : -70.56617199999999
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Short Mountain",
"short_name" : "Short Mountain",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Comté de Logan",
"short_name" : "Comté de Logan",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Arkansas",
"short_name" : "AR",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "États-Unis",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "72855",
"short_name" : "72855",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Paris, Arkansas 72855, États-Unis",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.3064849,
"lng" : -93.67508309999999
},
"southwest" : {
"lat" : 35.2672499,
"lng" : -93.7618069
}
},
"location" : {
"lat" : 35.2920325,
"lng" : -93.7299173
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.3064849,
"lng" : -93.67508309999999
},
"southwest" : {
"lat" : 35.2672499,
"lng" : -93.7618069
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
然后你解析JSON,并能够找到你想要的,与administrative_area_level_1
字段在这里的状态!
locality
字段:
http://maps.googleapis.com/maps/api/geocode/json?sensor=true&components=country:US|locality:Paris
过滤来自其他字段名称Paris
的可能结果
在angular中写了一个函数来获取地址
public formatAddress(googleAddr: any): any {
let properAddress: any = {
latitude: '',
longitude: '',
address: '',
premises: '',
area: '',
street: '',
locality: '',
state: '',
country: '',
timezone: '',
pincode: ''
};
let acutalPremise = '';
if (googleAddr && googleAddr.address_components) {
googleAddr.address_components.forEach((addr:any) => {
// country
if (addr.types.indexOf('country') != -1) {
properAddress.country = addr.long_name;
}
// state
if (addr.types.indexOf('administrative_area_level_1') != -1) {
properAddress.state = addr.long_name;
}
// Area
if (addr.types.indexOf('sublocality_level_1') != -1) {
properAddress.area = addr.long_name;
}
// locality
if (addr.types.indexOf('locality') != -1) {
properAddress.locality = addr.long_name;
}
// street
if (addr.types.indexOf('route') != -1) {
properAddress.street = addr.long_name;
}
// acutal address premises
if (addr.types.indexOf('premise') != -1) {
acutalPremise = addr.long_name;
}
// postal code
if (addr.types.indexOf('postal_code') != -1) {
properAddress.pincode = addr.long_name;
}
});
}
// get the timezone
properAddress.latitude = googleAddr.geometry.location.lat();
properAddress.longitude = googleAddr.geometry.location.lng();
properAddress.address = googleAddr.name + ', ' + googleAddr.formatted_address;
properAddress.premises = properAddress.street || acutalPremise ? googleAddr.name : '';
return properAddress;
}
- 如何在Google柱状图中动态添加行/列
- 从城市名称获取惊喜
- Google/html5语音识别JavaScript SDK Chrome网络工具包SpeechRecognition
- 使用Google Visualization动态调用构造函数
- Firebase2(Firebase.google.com)推送通知-从外部管理
- 是否可以控制获取哪些Google地图脚本(JavaScript API)
- 通过命令行/批处理文件打开页面时,将javascript代码注入Google Chrome
- 州和城市选择框类似于国家细分页面
- Google Adsense多次加载脚本
- 单击超链接时,如何使用Google Maps API v3缩放地图
- Google电子表格getValue([cell containing ])不返回制表符
- 实现一个建立在google.com之上的自定义搜索引擎
- 我应该在Google Api javascript中进行什么更改,以便它只能显示印度城市,而不能落后于州和印度
- 仅通过 Google 地图 API 自动完成功能获取城市名称
- 使用Google api获取城市的州/国家建议
- Google Places JS API,特定城市的街道名称
- 使用Google api匹配城市和州
- PHP、MySQL和Google Maps API按城市和州名称生成POI
- Google Geocoder为几个不同的地址返回同一组lat/lng.Lat/Lng是城市或乡镇的坐标,而不是地址
- 使用Google地理编码API从州/省获取城市/城镇列表