回答
使用JavaScript谷歌地圖V3 API:
您需要引用此:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
這樣做:=
var zip = <yourzipcode>;
var lat;
var lng;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': zip }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
geocoder.geocode({'latLng': results[0].geometry.location}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var loc = getCityState(results);
}
}
});
}
});
function getCityState(results)
{
var a = results[0].address_components;
var city, state;
for(i = 0; i < a.length; ++i)
{
var t = a[i].types;
if(compIsType(t, 'administrative_area_level_1'))
state = a[i].long_name; //store the state
else if(compIsType(t, 'locality'))
city = a[i].long_name; //store the city
}
return (city + ', ' + state)
}
function compIsType(t, s) {
for(z = 0; z < t.length; ++z)
if(t[z] == s)
return true;
return false;
}
這一切都會以此格式返回一個包含城市和州的字符串,但您可以根據需要調整它。
實測值上Css-tricks.com的製品,功能是使用Ziptastic http://css-tricks.com/using-ziptastic/
$.ajax({
url: "http://zip.elevenbasetwo.com",
cache: false,
dataType: "json",
type: "GET",
data: "zip=" + el.val(),
success: function(result, success) {
$(".fancy-form div > div").slideDown(); /* Show the fields */
$("#city").val(result.city); /* Fill the data */
$("#state").val(result.state);
$(".zip-error").hide(); /* In case they failed once before */
$("#address-line-1").focus(); /* Put cursor where they need it */
},
error: function(result, success) {
$(".zip-error").show(); /* Ruh row */
}
});
這不工作了,因爲跨產地問題.. – Ruchi 2015-06-10 04:48:47
@Ruchi我還沒有嘗試,但我認爲你可以使用「jsonp」來克服交叉來源問題。 – 2015-07-22 11:18:52
實現的紅線具有郵編APIthat執行此是免費的。他們在這裏有示例代碼http://zipcodedistanceapi.redline13.com/Examples。
所有的答案讚賞,如果你不想陷入更復雜的代碼,這裏是你的解決方案。
第一步:
下載最新全國郵政編碼的Excel文件,然後找到根文件夾下的Excel文件(你可以在這裏下載)
https://data.gov.in/catalog/all-india-pincode-pirectory#web_catalog_tabs_block_10)
第二步:
呼叫阿賈克斯功能你的表格在哪裏
<script src=」http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type=」text/javascript」><br>
jQuery(document).ready(function(){
jQuery(‘.postcode‘).blur(function(){ //.postcode class of zipcode text field
var s = jQuery(this).val();
jQuery.ajax({
type: ‘POST’,
url:「http://www.sample.com/postcode.php「, //file which read zip code excel file
dataType: 「json」, //is used for return multiple values
data: { ‘s’ : s },
success: function(data){
try {
jQuery(「.state「).val(data.state); //region-class of state text field
jQuery(「.city「).val(data.dist);//city-class of city text filed
} catch (e) {
alert(e.Message);
}
},
error:function (xhr, status, err){
alert(「status=」 + xhr.responseText + 「, error=」 + err);
}
});
});
});
</script>
這阿賈克斯功能將稱之爲「http://www.sample.com/postcode.php」文件
下,我們必須讀取Excel文件並返回狀態和城市價值
postcode.php
<?php
extract ($_POST);
$s=$_POST[‘s’]; //get value from ajax
$filename=」zip.csv「; //zipcode csv file(must reside in same folder)
$f = fopen($filename, 「r」);
while ($row = fgetcsv($f))
{
if ($row[1] == $s) //1 mean number of column of zipcode
{
$district=$row[3]; //3- Number of city column
$state=$row[4]; //4-Number of state column
break;
}
}
fclose($f);
echo json_encode(
array(「dist」 => $district,
「state」 => $state,
「zip」 => $s)
); //Pass those details by json
?>
這是postcode.php文件全部,享受
- 1. 如何自動填寫城市和州的表單字段?
- 2. 爲城市/州
- 3. 自動更正城市和州名
- 4. 使用jquery提取地址到城市,州,國家,郵編
- 5. 免費城市/州/郵編到緯度/經度數據庫?
- 6. 按城市,州或郵政編碼的接近搜索
- 7. GNU桑達格式美國地址,街道,城市,州,郵編
- 8. GeoDjango內置 - 按城市,州或郵政編碼
- 9. 郵政編碼到城市/州查找XML文件?
- 10. 谷歌地圖搜索城市/州或郵編
- 11. 你把地址分成街道/城市/州/郵編嗎?
- 12. 正則表達式地址/郵編/城市和州
- 13. 區分地址,城市,州,郵編在UISearchBar
- 14. 找到一個城市的州,州
- 15. 三個變量重寫規則與城市,州和郵政編碼,
- 16. Google自動填充到特定城市
- 17. 使用Google API生成基於城市和州的郵政編碼
- 18. 獲取來自城市和州/省或郵政編碼的時區偏移量
- 19. 與國家,城市等自動填寫表格
- 20. 自動完成Ajax - 按州列出城市完整列表?
- 21. 用於驗證城市/州/郵政編碼的正則表達式
- 22. 如何驗證天氣應用的郵政編碼和城市/州
- 23. 所有國家/地區各州應用中的城市和郵政編碼
- 24. TreeView。美國各州和城市
- 25. 國家,州和城市的PHP庫
- 26. 格式化城市/州信息
- 27. 如果選擇「美國」作爲國家,只驗證城市,州和郵編
- 28. 轉換地址(街道地址,城市,州,郵政編碼),以FIPS代碼
- 29. jQuery和/或PHP位置查找器:郵政編碼和城市/州
- 30. excel中分割地址,城市,州和郵編,缺少分隔符
什麼是我必須包括的其他參考。它出錯的錯誤:google.maps是未定義的 源文件:http://localhost/SWP/JavaScript/JqueryScriptSource.js 這一行:var geocoder = new google.maps.Geocoder(); – user695663 2011-05-16 21:27:27
你必須參考上面在編輯 – slandau 2011-05-16 21:32:06
這是什麼? 「compIsType」我得到以下錯誤錯誤:compIsType未定義 – slandau 2011-05-16 21:32:41