我在将 django 服务器的功能显示到 openlayers 上的矢量图层时遇到问题,我已经参考了这些文章:
在 GeoDjango 的自定义视图中渲染 GeoQuerySet 的空间数据
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:
我错过了什么吗?因为它仍然没有在地图上显示对象。