我已经成功使用 openlayers 和 OpenStreetMaps 与管理应用程序一起运行了 geodjango。
现在,我想编写一些视图来显示数据。基本上,我只想在地图上添加一个点列表(在管理员中看到的点)。
似乎 Geodjango 使用一个openlayers.js文件在管理页面实现其功能。是否有一种良好的方式与之交互?
如何编写视图/模板以在开放街道地图窗口中显示 geodjango 数据,就像在管理员中看到的那样?
目前,我正在研究openlayers.js 文件和 api,寻找“简单”的解决方案。(我没有 js 经验,所以这需要一些时间。)
目前我能看到的做法是添加以下内容作为模板,并使用 Django 添加所需的代码来显示点。(参考此处的示例)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Draw Feature Example</title>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<script type="text/javascript">
var map;
function init(){
map = new OpenLayers.Map('map');
var layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} );
map.addLayer(layer);
/*
* Layer style
*/
// we want opaque external graphics and non-opaque internal graphics
var layer_style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
layer_style.fillOpacity = 0.2;
layer_style.graphicOpacity = 1;
/*
* Blue style
*/
var style_blue = OpenLayers.Util.extend({}, layer_style);
style_blue.strokeColor = "blue";
style_blue.fillColor = "blue";
style_blue.graphicName = "star";
style_blue.pointRadius = 10;
style_blue.strokeWidth = 3;
style_blue.rotation = 45;
style_blue.strokeLinecap = "butt";
var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry", {style: layer_style});
// create a point feature
var point = new OpenLayers.Geometry.Point(-111.04, 45.68);
var pointFeature = new OpenLayers.Feature.Vector(point,null,style_blue);
// Add additional points/features here via django
map.addLayer(vectorLayer);
map.setCenter(new OpenLayers.LonLat(point.x, point.y), 5);
vectorLayer.addFeatures([pointFeature]);
}
</script>
</head>
<body onload="init()">
<div id="map" class="smallmap"></div>
</body>
</html>
这是正确的做法吗?还是有更好的方法?