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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28596237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:46:09  来源:igfitidea点击:

Loading a geoJSON object directly into google maps v3

jsongoogle-mapsgoogle-maps-api-3geojson

提问by Itinerati

I'm trying to create a map, using floor plans that I've stored in mongodb. If I put the JSON into a file, I can call it using map.data.loadGeoJson('myfile.json')

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

However, I don't want to save a file each time I build a map, and I'd rather write an object directly. Something like this:

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

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);

Doing that doesn't work. Is there some other way to load everything from a single object, or do I need to save them to a file / construct individual polygons using google.maps.Polygon()?

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

回答by iH8

Use the addGeoJsonmethod of datainstead of loadGeoJson. loadGeoJsonexpects an URL as parameter, not a GeoJSON featurecollection object.

使用代替的addGeoJson方法。需要一个 URL 作为参数,而不是一个 GeoJSON 特征集合对象。dataloadGeoJsonloadGeoJson

map.data.addGeoJson(tempObject);

Check the reference: https://developers.google.com/maps/documentation/javascript/reference#Data

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