是否可以在javascript中的客户端上实现一个函数,以便像这样使用它:
var county = get_county(zip_code);
我的意思是可以通过脚本在线完成吗?或者我必须挖一些数据库?
或者我必须买它?
任何帮助表示赞赏!
P.S。
在Dyrandz Famador的帮助下,我为GAS做了这个功能:
function get_county(zip) {
var county, response, result, adresses, i, j, n, type;
response = Maps.newGeocoder().geocode(zip);
for (i = 0; i < response.results.length; i++) {
result = response.results[i];
adresses = result.address_components;
n = adresses.length;
for (j=0;j<n;j++) {
type = adresses[j].types[0];
if (type == 'locality') county = (adresses[j].long_name);
if (type == 'administrative_area_level_2') {county =(adresses[j].long_name);}
}
}
return (county);
}
答案 0 :(得分:5)
您可以使用Google Maps API从邮政编码中获取地点
Google提供了许多API,其中包括Maps API。在这个例子中 我们将显示使用邮政编码打到Google所需的所有代码 以城市,州和国家的形式获得位置。
首先要做的是参考Google的Map API:
<script language="javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
接下来是一个用于输入邮政编码的小表格:
<form>
zip: <input type="text" name="zip" value="46032"> <a href="#" onclick="getLocation()">Get Address</a>
</form>
你可以看到我有一个激活名为“getLocation()”
的函数的链接 - 下面你会找到该函数和相关代码:
...
<script language="javascript">
function getLocation(){
getAddressInfoByZip(document.forms[0].zip.value);
}
function response(obj){
console.log(obj);
}
function getAddressInfoByZip(zip){
if(zip.length >= 5 && typeof google != 'undefined'){
var addr = {};
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': zip }, function(results, status){
if (status == google.maps.GeocoderStatus.OK){
if (results.length >= 1) {
for (var ii = 0; ii < results[0].address_components.length; ii++){
var street_number = route = street = city = state = zipcode = country = formatted_address = '';
var types = results[0].address_components[ii].types.join(",");
if (types == "street_number"){
addr.street_number = results[0].address_components[ii].long_name;
}
if (types == "route" || types == "point_of_interest,establishment"){
addr.route = results[0].address_components[ii].long_name;
}
if (types == "sublocality,political" || types == "locality,political" || types == "neighborhood,political" || types == "administrative_area_level_3,political"){
addr.city = (city == '' || types == "locality,political") ? results[0].address_components[ii].long_name : city;
}
if (types == "administrative_area_level_1,political"){
addr.state = results[0].address_components[ii].short_name;
}
if (types == "postal_code" || types == "postal_code_prefix,postal_code"){
addr.zipcode = results[0].address_components[ii].long_name;
}
if (types == "country,political"){
addr.country = results[0].address_components[ii].long_name;
}
}
addr.success = true;
for (name in addr){
console.log('### google maps api ### ' + name + ': ' + addr[name] );
}
response(addr);
} else {
response({success:false});
}
} else {
response({success:false});
}
});
} else {
response({success:false});
}
}
</script>
...
就是这样 - 在Chrome中打开控制台并在输入不同的邮政编码时注意输出。
从这里得到它: http://rickluna.com/wp/2012/09/using-the-google-maps-api-to-get-locations-from-zip-codes/
答案 1 :(得分:0)
你需要知道zip_code和country的映射,
我为此找到了一个github项目:
https://github.com/jgoodall/us-maps
一旦你通过指令获得json映射,就可以实现你想要的功能。
答案 2 :(得分:0)
这是来自美国政府的一个很好的地理数据库API:
https://www.sba.gov/about-sba/sba-performance/sba-data-store/web-service-api/us-city-and-county-web-data-api#county-state
API会生成所有县和州信息,但它似乎没有包含邮政编码。使用Google Geocoding API可能会更容易。
答案 3 :(得分:0)
这是您可以从几乎任何位置输入中获取地址,城市,州和/或邮政编码的一种方式,例如Santa Monica, CA
或90405
。请记住,除非您在请求中非常具体,否则您无法始终获得所有这些变量。
function getLocationInfo(){
var location = $('#location').val();
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': location }, function (result, status) {
var address = '';
var streetNumber = '';
var streetName = '';
var cityName = '';
var stateName = '';
var zipCode = '';
var addressComponent = result[0]['address_components'];
// find street number
var streetNumberQueryable = $.grep(addressComponent, function (x) {
return $.inArray('street_number', x.types) != -1;
});
if (streetNumberQueryable.length) {
streetNumber = streetNumberQueryable[0]['long_name'];
}
// find street name
var streetNameQueryable = $.grep(addressComponent, function (x) {
return $.inArray('route', x.types) != -1;
});
if (streetNameQueryable.length) {
streetName = streetNameQueryable[0]['long_name'];
}
// find city data
var cityQueryable = $.grep(addressComponent, function (x) {
return $.inArray('locality', x.types) != -1;
});
if (cityQueryable.length) {
cityName = cityQueryable[0]['long_name'];
}
// find state data
var stateQueryable = $.grep(addressComponent, function (x) {
return $.inArray('administrative_area_level_1', x.types) != -1;
});
if (stateQueryable.length) {
stateName = stateQueryable[0]['long_name'];
}
// find zip data
var zipQueryable = $.grep(addressComponent, function (x) {
return $.inArray('postal_code', x.types) != -1;
});
if (zipQueryable.length) {
zipCode = zipQueryable[0]['long_name'];
}
address = streetNumber + ' ' + streetName;
console.log(address);
console.log(cityName);
console.log(stateName);
console.log(zipCode);
});
}