Reputation: 25
I'm having trouble displaying features from my django server to a vector layer on openlayers, I've already referenced these articles:
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
but still can't make it show.
so here is the javascript block:
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();
}
}
and here are my views that call the GeoJSON object:
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")
and upon checking on firebug, there the json was requested:
Am i missing something? cause it still doesn't show the objects on the map.
Upvotes: 0
Views: 742
Reputation: 436
If your vector layer is loaded but not displayed it is probably a projection problem.
I would gess that OSM's EPSG is not 4326 but 900913 !
Upvotes: 1
Reputation: 81
Make sure the path_json is not broken. If you have more than one item, it should be a featured collection.
Upvotes: 0