有效的geoJson不会填充Google地图标记

时间:2014-12-03 18:01:45

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

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

下面我正在运行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]
        }
      ]
    }

1 个答案:

答案 0 :(得分: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]
      }
    }
  ]
}