我在搜索 SO: Zip Code to City/State 时遇到了这个问题,反之亦然?
但我想知道,为什么我们不能为此使用 Maps API?有没有人试过这样做?
我在搜索 SO: Zip Code to City/State 时遇到了这个问题,反之亦然?
但我想知道,为什么我们不能为此使用 Maps API?有没有人试过这样做?
是的,使用 Google Maps Geocoding 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 代码游乐场在线尝试一下。
玩得开心!
使用谷歌地图 api Geocoder
为此,使用Geonames网络服务 api非常容易,特别是 postalCodeSearch方法。根据我的经验,除非您每秒发出多个查询,否则效果很好,尽管缓存结果通常是值得的。(即:它们不会经常更改。)