简体   繁体   English

谷歌地图与县覆盖?

[英]Google Maps with counties overlay?

I have no experience working with Google Maps, but I'd like to embed a "Choose your county" map in my webapp. 我没有使用谷歌地图的经验,但我想在我的网络应用程序中嵌入“选择你的县”地图。 My app only targets California, so it only needs to support one state, but I can't find any easy way to do this (without manually drawing overlay polygons for all the county lines). 我的应用程序仅针对加利福尼亚州,因此它只需要支持一个州,但我找不到任何简单的方法来执行此操作(无需为所有县行手动绘制叠加多边形)。

I assumed I'd easily be able to find examples of "Choose your county"-style interfaces on Google somehow, but the only thing I found was this -- http://maps.huge.info/county.htm -- and I'm leaning towards hitting their API to pull the geometry for the whole state if I can't find a better option. 我以为我很容易就能在某种程度上找到Google上“选择你的县”风格界面的例子,但我发现的唯一一件事就是这个 - http://maps.huge.info/county.htm - 和如果我找不到更好的选择,我倾向于使用他们的API来拉动整个状态的几何。

Does anyone have any experience or insight to help make this a little easier? 有没有人有任何经验或见解帮助使这更容易一点?

The Google Maps API does not provide county polygons, or any other predefined borders of states or countries. Google Maps API不提供县多边形或州或国家/地区的任何其他预定义边框。 Therefore the main issue here is obtaining the polygon data. 因此,这里的主要问题是获得多边形数据。

A polygon on the Google Maps API is defined by constructing an array of LatLng objects (assuming you're using the v3 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)
];    

Then you would use this array to construct a Polygon object: 然后,您将使用此数组来构造Polygon对象:

var bermudaTriangle = new google.maps.Polygon({
  paths: bermudaTriangleCoords,
  strokeColor: '#FF0000',
  strokeOpacity: 0.8,
  strokeWeight: 2,
  fillColor: '#FF0000',
  fillOpacity: 0.35
});

And finally show it on the map by calling the setMap() method: 最后通过调用setMap()方法在地图上显示它:

bermudaTriangle.setMap(map);    // Assuming map is your google.maps.Map object

If you keep a reference to your Polygon object, you can also hide it by passing null to the setMap() method: 如果保留对Polygon对象的引用,还可以通过将null传递给setMap()方法来隐藏它:

bermudaTriangle.setMap(null); 

Therefore you could consider building a JavaScript object with a property name for each county. 因此,您可以考虑使用每个县的属性名称构建JavaScript对象。 This will allow you to fetch the polygon objects from the name of the counties in O(1) (constant time), without having to iterate through all the collection. 这将允许您从O(1)中的县名(常量时间)中获取多边形对象,而无需遍历所有集合。 Consider the following example: 请考虑以下示例:

// 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...

The counties object will be our data store. counties对象将是我们的数据存储。 When a user searches for "El Dorado" you can simply show the polygon as follows: 当用户搜索“El Dorado”时,您只需按如下方式显示多边形:

counties['El Dorado'].setMap(map);

If you keep a reference to the previously searched county, you can also call setMap(null) to hide the previous polygon: 如果保留对先前搜索的县的引用,还可以调用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');
}

I hope this gets you going in the right direction. 我希望这能让你朝着正确的方向前进。

Google Fusion Tables now links to State, County, and Congressional District data for all of the US. Google Fusion Tables现在可以链接到美国各州的州,郡和国会区数据

The geometry field of the fusion table contains a Polygon element for the given object. 融合表的几何字段包含给定对象的Polygon元素。 Their state boundaries polygons (and to a lesser extent the counties) look a little low resolution in a few places, but it may be good enough for you and should be relatively easy to grab. 他们的状态边界多边形(以及较小程度上的县)在一些地方看起来有点低分辨率,但它可能对你来说足够好并且应该相对容易抓住。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM