【问题标题】:Valid geoJson not populating Google Maps markers有效的 geoJson 未填充 Google 地图标记
【发布时间】:2014-12-03 18:01:44
【问题描述】:

与我的 geoJson 关联的标记没有填充在我的地图上。如果我在 http://geojsonlint.com/ 上运行我的 geoJSON,一切正常。如果我用来自 google maps dev api 'https://storage.googleapis.com/maps-devrel/google.json' 的示例交换我的 geoJSON,他们的叠加层会很好地填充在我的地图上。

下面我正在运行 http://localhost:3009/murals.json 作为 loadGeoJson 的参数我还尝试从本地文件运行 test.json。

我的地图.js

function initialize() {
  var myLatlng = new google.maps.LatLng(40.0172679,-105.2839094);
  var myOptions = {
    zoom: 16,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(
    document.getElementById('map'), myOptions
  );
  map.data.loadGeoJson('http://localhost:3009/murals.json');
};

google.maps.event.addDomListener(window, "load", initialize());

我的控制器(毫无疑问可以重构,但输出格式正确的 geoJSON)

def index
    @murals = Mural.all
    muralHash = []
    @geojson = { type: "GeometryCollection",
      geometries: muralHash
    }
      @murals.each do |mural, myHash = {:type => nil,:coordinates => nil}|
        myHash["type"] = 'Point'
        myHash["coordinates"] = [mural.longitude, mural.latitude]

        muralHash << myHash
      end
    respond_to do |format|
      format.html
      format.json { render json: @geojson }
    end
  end

geoJSON

    { "type":"GeometryCollection",
      "geometries":[
        {
          "type":"Point",
          "coordinates":[-105.287685950293,40.0124034482671]
        },
        {
          "type":"Point",
          "coordinates":[-105.196297724738,39.9935339839196]
        },
        {
          "type":"Point",
          "coordinates":[-105.283136923804,40.0162490232761]
        }
      ]
    }

【问题讨论】:

  • 请发布 JSON

标签: javascript ruby-on-rails json google-maps


【解决方案1】:

JSON 无效(与 maps-API 预期的格式相关的有效),这会起作用:

{
  "type":"FeatureCollection",
  "features":[
    {
      "type":"Feature",
      "geometry":{
        "type":"Point",
        "coordinates":[-105.287685950293,40.0124034482671]
      }
    },
    {
      "type":"Feature",
      "geometry":{
        "type":"Point",
        "coordinates":[-105.196297724738,39.9935339839196]
      }
    },
    {
      "type":"Feature",
      "geometry":{
        "type":"Point",
        "coordinates":[-105.283136923804,40.0162490232761]
      }
    }
  ]
}

【讨论】:

  • 这对我不起作用。此外,当我在 geoJSON linter 中运行它时,它也会失败。
  • 必须对我的代码进行一些修改才能暂时将其放入其中,但它正在运行。我认为相信 geoJSON linter 是一个坏举动?
猜你喜欢
  • 2015-02-21
  • 2014-09-17
  • 2014-06-01
  • 2019-09-10
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
相关资源
最近更新 更多