将geoJSON对象直接加载到谷歌地图v3中

时间:2015-02-18 23:52:53

标签: json google-maps google-maps-api-3 geojson

我正在尝试使用我存储在mongodb中的平面图来创建地图。如果我将JSON放入文件中,我可以使用map.data.loadGeoJson('myfile.json')

来调用它

但是,每次构建地图时我都不想保存文件,而是宁愿直接写一个对象。像这样:

var tempObject = {
    "type": "FeatureCollection",
    "features": [
      {
        "type": "Feature",
        "properties": {
          "letter": "G",
          "color": "blue",
          "rank": "7",
          "ascii": "71"
        },
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [123.61, -22.14], [122.38, -21.73], [121.06, -21.69], [119.66, -22.22], [119.00, -23.40],
              [118.65, -24.76], [118.43, -26.07], [118.78, -27.56], [119.22, -28.57], [120.23, -29.49],
              [121.77, -29.87], [123.57, -29.64], [124.45, -29.03], [124.71, -27.95], [124.80, -26.70],
              [124.80, -25.60], [123.61, -25.64], [122.56, -25.64], [121.72, -25.72], [121.81, -26.62],
              [121.86, -26.98], [122.60, -26.90], [123.57, -27.05], [123.57, -27.68], [123.35, -28.18],
              [122.51, -28.38], [121.77, -28.26], [121.02, -27.91], [120.49, -27.21], [120.14, -26.50],
              [120.10, -25.64], [120.27, -24.52], [120.67, -23.68], [121.72, -23.32], [122.43, -23.48],
              [123.04, -24.04], [124.54, -24.28], [124.58, -23.20], [123.61, -22.14]
            ]
          ]
        }
      }
    ]
  };


  map.data.loadGeoJson(tempObject);

这样做不起作用。是否有其他方法可以从单个对象加载所有内容,或者我是否需要使用google.maps.Polygon()将它们保存到文件/构建单个多边形?

1 个答案:

答案 0 :(得分:60)

使用addGeoJson的{​​{1}}方法代替dataloadGeoJson期望将URL作为参数,而不是GeoJSON featurecollection对象。

loadGeoJson

检查参考:https://developers.google.com/maps/documentation/javascript/reference#Data