我碰到這個來的時候我做了一個SO搜索: Zip Code to City/State and vice-versa in a database?我可以使用Google/Yahoo Maps API根據zip查找城市/州嗎?
但我想知道,爲什麼我們不能只使用地圖API爲這個?有沒有人試過這樣做?
我碰到這個來的時候我做了一個SO搜索: Zip Code to City/State and vice-versa in a database?我可以使用Google/Yahoo Maps API根據zip查找城市/州嗎?
但我想知道,爲什麼我們不能只使用地圖API爲這個?有沒有人試過這樣做?
爲此,使用Geonames webservice apis相當容易,特別是the postalCodeSearch方法。雖然緩存結果通常是值得的,但除非您每秒發出多個查詢,否則在我的經驗中效果很好。 (即:不經常更改。)
使用谷歌地圖API Geocoder
是的,這是很容易與谷歌地圖地理編碼getLocations()做到這一點。
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Geocoding example</title>
<script src="http://maps.google.com/maps?file=api&v=2" type="text/javascript"></script>
<script type="text/javascript">
var geocoder;
var map;
var address = "10001"; //NEW YORK zip
function load()
{
map = new GMap2(document.getElementById("map"));
geocoder = new GClientGeocoder();
geocoder.getLocations(address, addToMap);
}
function addToMap(response)
{
place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
map.setCenter(point, 13);
marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(place.address);
}
</script>
</head>
<body onload="load()" onunload="GUnload()">
<div id="map" style="width: 500px; height: 500px"></div>
</body>
</html>
您可以試試Google代碼playground。
玩得開心!