[英]Google Maps with counties overlay?
我没有使用谷歌地图的经验,但我想在我的网络应用程序中嵌入“选择你的县”地图。 我的应用程序仅针对加利福尼亚州,因此它只需要支持一个州,但我找不到任何简单的方法来执行此操作(无需为所有县行手动绘制叠加多边形)。
我以为我很容易就能在某种程度上找到Google上“选择你的县”风格界面的例子,但我发现的唯一一件事就是这个 - http://maps.huge.info/county.htm - 和如果我找不到更好的选择,我倾向于使用他们的API来拉动整个状态的几何。
有没有人有任何经验或见解帮助使这更容易一点?
Google Maps API不提供县多边形或州或国家/地区的任何其他预定义边框。 因此,这里的主要问题是获得多边形数据。
Google Maps API上的多边形是通过构造LatLng
对象数组来定义的(假设您使用的是v3 API):
var bermudaTriangleCoords = [
new google.maps.LatLng(25.774252, -80.190262),
new google.maps.LatLng(18.466465, -66.118292),
new google.maps.LatLng(32.321384, -64.757370),
new google.maps.LatLng(25.774252, -80.190262)
];
然后,您将使用此数组来构造Polygon
对象:
var bermudaTriangle = new google.maps.Polygon({
paths: bermudaTriangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
最后通过调用setMap()
方法在地图上显示它:
bermudaTriangle.setMap(map); // Assuming map is your google.maps.Map object
如果保留对Polygon
对象的引用,还可以通过将null
传递给setMap()
方法来隐藏它:
bermudaTriangle.setMap(null);
因此,您可以考虑使用每个县的属性名称构建JavaScript对象。 这将允许您从O(1)中的县名(常量时间)中获取多边形对象,而无需遍历所有集合。 请考虑以下示例:
// Let's start with an empty object:
var counties = {
};
// Now let's add a county polygon:
counties['Alameda'] = new google.maps.Polygon({
paths: [
// This is not real data:
new google.maps.LatLng(25.774252, -80.190262),
new google.maps.LatLng(18.466465, -66.118292),
new google.maps.LatLng(32.321384, -64.757370),
new google.maps.LatLng(25.774252, -80.190262)
// ...
],
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.3
});
// Next county:
counties['Alpine'] = new google.maps.Polygon({
// Same stuff
});
// And so on...
counties
对象将是我们的数据存储。 当用户搜索“El Dorado”时,您只需按如下方式显示多边形:
counties['El Dorado'].setMap(map);
如果保留对先前搜索的县的引用,还可以调用setMap(null)
来隐藏前一个多边形:
var userInput = 'El Dorado';
var latestSearch = null;
// Check if the county exists in our data store:
if (counties.hasOwnProperty(userInput)) {
// It does - So hide the previous polygon if there was one:
if (latestSearch) {
latestSearch.setMap(null);
}
// Show the polygon for the searched county:
latestSearch = counties[userInput].setMap(map);
}
else {
alert('Error: The ' + userInput + ' county was not found');
}
我希望这能让你朝着正确的方向前进。
Google Fusion Tables现在可以链接到美国各州的州,郡和国会区数据 。
融合表的几何字段包含给定对象的Polygon元素。 他们的状态边界多边形(以及较小程度上的县)在一些地方看起来有点低分辨率,但它可能对你来说足够好并且应该相对容易抓住。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.