Django + Openlayers GeoJSON没有显示

时间:2013-07-10 06:25:16

标签: django json openlayers geodjango geojson

我在将django服务器中的功能显示到openlayers上的矢量图层时遇到问题, 我已经引用了这些文章:

Rendering spatial data of GeoQuerySet in a custom view on GeoDjango

https://gis.stackexchange.com/questions/22529/trouble-displaying-geojson-file-in-openlayers?rq=1

但仍然无法展示。

所以这里是javascript块:

  function init(){
      var map = new OpenLayers.Map('map', {
      projection: new OpenLayers.Projection("EPSG:3857"),
      units: "km",
      maxResolution: 156543.0339,
      displayProjection: new OpenLayers.Projection("EPSG:4326"),
      controls: [
        new OpenLayers.Control.Navigation(),
        new OpenLayers.Control.KeyboardDefaults(),
        new OpenLayers.Control.PanZoomBar(),
        new OpenLayers.Control.Scale(),
        new OpenLayers.Control.Attribution()
      ]
    });

    var osm_layer = new OpenLayers.Layer.OSM("OpenStreetMap");
    map.addLayer(osm_layer);

    var vector_style = new OpenLayers.Style({
      strokeWidth:2,
      fillOpacity:0,
      strokeColor: '#008000'
    });

    var vector_style_map = new OpenLayers.StyleMap({
         'default': vector_style,
         'select': {strokeColor: '#0000FF'}
    });

    var path_layer = new OpenLayers.Layer.Vector("Path Layer", {
      protocol: new OpenLayers.Protocol.HTTP({
        url: "{% url 'get_path_json' route.id %}",
        format: new OpenLayers.Format.GeoJSON({
          internalProjection: new OpenLayers.Projection("EPSG:3857"),
          externalProjection: new OpenLayers.Projection("EPSG:4326")})
      }),
      strategies: [new OpenLayers.Strategy.Fixed()],
      styleMap: vector_style_map
    });

    map.addLayer(path_layer);

    map.addControl(new OpenLayers.Control.LayerSwitcher());
    map.setCenter(
    new OpenLayers.LonLat(121.032, 14.594).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()), 12);
    if(!map.getCenter()){
      map.zoomToMaxExtent();
    }
  }

以下是调用GeoJSON对象的视图:

def get_path_json(request, route_id):
    route = get_object_or_404(Route, pk=route_id)
    geoj = GeoJSON.GeoJSON()
    path = route.path.all()

    path_format = Django.Django(geodjango="path")
    path_json = geoj.encode(path_format.decode(path))

    return HttpResponse(path_json, content_type="application/json")

在查看萤火虫时,请求json:

enter image description here 我错过了什么吗?因为它仍然没有在地图上显示对象。

2 个答案:

答案 0 :(得分:1)

如果您的矢量图层已加载但未显示,则可能是投影问题。

我认为OSM的EPSG不是4326而是900913!

答案 1 :(得分:0)

确保path_json没有损坏。如果您有多个项目,则应该是精选集合。