2013-07-10 67 views
0
我有显示从我的Django的服务器功能上的OpenLayers矢量图层麻烦

, 我已经引用这些文章:的Django +的OpenLayers以GeoJSON不显示

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

,并在上Firebug的检查,有要求JSON:

enter image description here 我错过了什么吗?导致它仍然不显示地图上的对象。

回答

0

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

1

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

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