Google Maps API:添加邮政编码边界

Google Maps API: adding zip code boundaries

本文关键字:邮政编码 边界 添加 Maps API Google      更新时间:2023-09-26

我正在使用Google Maps JS API在地图上放置一些标记。代码非常简单。我只是使用"new google.maps"创建地图。"new google.maps.Marker",然后用"new google.maps.Marker"创建每个标记。

除了显示标记之外,我还希望地图显示一个特定的美国邮政编码的边界。我发现,如果你去一个类似https://www.google.com/maps/place/14618或https://www.google.com/maps/place/94703的URL,谷歌地图网站会显示一个美国邮政编码的边界。

谁能告诉我如何使用谷歌地图API来显示美国邮政编码边界?

您需要一个邮政编码边界的源代码。例如,它可以是邮政编码制表区域。美国人口普查局提供kml文件,谷歌地图API有KmlLayer,可以像这样放在一起:

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 11,
  center: {lat: 41.876, lng: -87.624}
})
var ctaLayer = new google.maps.KmlLayer({
  url: 'http://anatolysukhanov.com/94703.kml',
  map: map
})
#map {
  height: 100%;
}
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <title>KML Layers</title>
  </head>
  <script src="https://maps.googleapis.com/maps/api/js"></script>
  <body>
    <div id="map"></div>
  </body>
</html>