在Google地图上显示OpenStreetMap边界(使用v3 api)

时间:2016-10-14 17:55:15

标签: google-maps google-maps-api-3 openstreetmap

我想使用GeoJSON数据在Google地图上显示城市边界,我使用以下工具。

  1. 我去了nominatim.openstreetmap.org并搜索了一个城市(例如丹佛)
  2. 使用检索到的OSM ID(在本例中为253750),然后我使用在线工具(polygons.openstreetmap.fr)生成了GeoJSON文件 - 看起来像这样http://polygons.openstreetmap.fr/get_geojson.py?id=253750&params=0
  3. 之后,我已经下载了该文件,并尝试使用简单的方法将此GeoJSON数据加载到我的地图中:

    map.data.loadGeoJson(
                'http://domain.com/example.json');
    

    但是,在我的控制台中我收到了一个错误:

    Uncaught InvalidValueError: not a Feature or FeatureCollection
    

    问题是即使上面的工具生成GeoJSON格式,它也不被认为是标准格式。那么我们之间是否会有一个额外的步骤,或者我错过了什么?

1 个答案:

答案 0 :(得分:4)

相关问题:google maps addGeoJson from text field with js

您需要获取从Open Street Maps检索的数据并将其放入GeoJson解析器所期望的Feature / FeatureCollection JSON中的位置:

var geoJson = {
  "type": "FeatureCollection",
  "features": [{
    "type": "Feature",
    "geometry": {},
    "properties": {}
  }];
// where "geoJsonData" is the data you retrieved
geoJson.features[0].geometry = geoJsonData;

example fiddle (with the data from the posted link)