2013-07-10 72 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!