我需要显示一个交互式地图,邮政编码显示其边界,并为邮政编码设置不同的颜色,如下所示:
http://www.usnaviguide.com/zip.htm
输入美国邮政编码,然后点击“查找邮政编码”。
也许我忽略了它,但我没有找到这方面的例子和文档在Google Maps API文档中专门讨论这个问题。我正在尝试在上面的网页链接上做一个视图源,但它对我来说似乎并不明显它是如何工作的。页面上还有其他内容,我不知道它是否是我需要与否的一部分。
一些简单的代码示例将非常有用!谢谢!
答案 0 :(得分:10)
以下是我与FusionTablesLayers合并的美国邮政编码地图:
http://www.geocodezip.com/geoxml3_test/v3_FusionTables_zipcode_map.html
它没有不同的颜色,但你可以改变它。
John Coryat使用磁贴服务器制作了该地图,您可以使用Google Maps API v3自定义地图执行此操作。
来自示例的代码段:
google.load('visualization', '1', {'packages':['corechart', 'table', 'geomap']});
var map;
var labels = [];
var layer;
var tableid = 1499916;
function initialize() {
geocoder = new google.maps.Geocoder();
map = new google.maps.Map(document.getElementById('map_canvas'), {
center: new google.maps.LatLng(37.23032838760389, -118.65234375),
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
layer = new google.maps.FusionTablesLayer(tableid);
layer.setQuery("SELECT 'geometry' FROM " + tableid);
layer.setMap(map);
codeAddress("11501" /*document.getElementById("address").value*/ );
google.maps.event.addListener(map, "bounds_changed", function() {
displayZips();
});
google.maps.event.addListener(map, "zoom_changed", function() {
if (map.getZoom() < 11) {
for (var i=0; i<labels.length; i++) {
labels[i].setMap(null);
}
}
});
}
function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
if (results[0].geometry.viewport)
map.fitBounds(results[0].geometry.viewport);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function displayZips() {
//set the query using the current bounds
var queryStr = "SELECT geometry, ZIP, latitude, longitude FROM "+ tableid + " WHERE ST_INTERSECTS(geometry, RECTANGLE(LATLNG"+map.getBounds().getSouthWest()+",LATLNG"+map.getBounds().getNorthEast()+"))";
var queryText = encodeURIComponent(queryStr);
var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq=' + queryText);
// alert(queryStr);
//set the callback function
query.send(displayZipText);
}
var infowindow = new google.maps.InfoWindow();
function displayZipText(response) {
if (!response) {
alert('no response');
return;
}
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
if (map.getZoom() < 11) return;
FTresponse = response;
//for more information on the response object, see the documentation
//http://code.google.com/apis/visualization/documentation/reference.html#QueryResponse
numRows = response.getDataTable().getNumberOfRows();
numCols = response.getDataTable().getNumberOfColumns();
/* var queryStr = "SELECT geometry, ZIP, latitude, longitude FROM "+ tableid + " WHERE ST_INTERSECTS(geometry, RECTANGLE(LATLNG"+map.getBounds().getSouthWest()+",LATLNG"+map.getBounds().getNorthEast()+"))";
*/
for(i = 0; i < numRows; i++) {
var zip = response.getDataTable().getValue(i, 1);
var zipStr = zip.toString()
while (zipStr.length < 5) { zipStr = '0' + zipStr; }
var point = new google.maps.LatLng(
parseFloat(response.getDataTable().getValue(i, 2)),
parseFloat(response.getDataTable().getValue(i, 3)));
// bounds.extend(point);
labels.push(new InfoBox({
content: zipStr
,boxStyle: {
border: "1px solid black"
,textAlign: "center"
,backgroundColor:"white"
,fontSize: "8pt"
,width: "50px"
}
,disableAutoPan: true
,pixelOffset: new google.maps.Size(-25, 0)
,position: point
,closeBoxURL: ""
,isHidden: false
,enableEventPropagation: true
}));
labels[labels.length-1].open(map);
}
// zoom to the bounds
// map.fitBounds(bounds);
}
google.maps.event.addDomListener(window,'load',initialize);
#map_canvas { width: 610px; height: 400px; }
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js"></script>
<script type="text/javascript" src="http://geoxml3.googlecode.com/svn/branches/polys/geoxml3.js"></script>
<form>
<span class="style51"><span class="style49">Show</span>:</span>
<input id="address" type="text" value="11501" ></input>
<input id="geocode" type="button" onclick="codeAddress(document.getElementById('address').value);" value="Geocode"></input>
<div id="map_canvas"></div>
答案 1 :(得分:1)
要绘制区域,您可以使用Google地图API中的Polygon,但您需要您想要绘制的州或城市边界数据或zipcodes。
您可以在该多边形示例中使用视图源,如果您已经使用了Google地图,则可以非常轻松地将Polygon Obj传递给LatLng objs数组并完成
关于我认为您可以在fusion table like this中找到的数据,这是美国邮政编码。
答案 2 :(得分:0)
我正在寻找可以在网站上使用的邮政编码地图。我偶然发现这个有点慢的免费项目,但完全符合我的要求: http://www.openheatmap.com/
非常适合个人使用,在向用户显示时效果不佳。如果有人和我的情况相同,我希望这可以帮助他们。
答案 3 :(得分:0)
用于获取所需内容的简单API: https://www.mashape.com/vanitysoft/boundaries-io
以上API按邮政编码,城市和州显示US Boundaries(GeoJson)。 您应该以编程方式使用API来处理大量结果。
例如: