我是Django / OpenLayers的新手,我一直在关注各种教程,尤其是this one。我使用一个调用内置GeoDjango模板的视图来动态呈现KML,我尝试将其传递给页面以使用OpenLayers呈现地图。但是,我得到的只是默认地图,就像我从未添加过' kml'我的map.html文件中的图层。我怀疑KML文件不会被加载到OpenLayers中,但我根本不理解为什么不能,或者我如何测试KML数据是否成功加载。或者,KML正在成功加载,但由于某种原因无法正确显示。
来自models.py的相关模型:
class Dialect(models.Model):
dialectCode = models.CharField("short code for location", max_length=10, unique=True, primary_key=True)
dialectNameEn = models.TextField("human readable name of the dialect")
dialectTag = models.ManyToManyField('DialectTag',blank=True, null=True)
locationName = models.TextField("human readable location name", blank=True)
locationNameAr = models.TextField("Arabic name of location if available", blank=True)
centerLoc = models.PointField("a point representing this dialect", srid=4326)
regionLoc = models.MultiPolygonField("multipolygon geometry", blank=True, null=True) #optional regional info
objects = models.GeoManager()
def __unicode__(self):
return self.dialectCode
我的views.py:
from django.shortcuts import render, render_to_response
from django.contrib.gis.shortcuts import render_to_kml
from dialectsDB.models import *
def all_kml(request):
locations = Dialect.objects.kml()
return render_to_kml("gis/kml/placemarks.kml", {'places' : locations})
# Create your views here.
def map_page(request):
lcount = Dialect.objects.all().count()
return render_to_response('map.html', {'location_count' : lcount})
简单的urls.py:
from django.conf.urls import patterns, include, url
from dialectsDB.views import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'dialects.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^kml/', all_kml),
url(r'^map/', map_page)
)
map.html文件:
<html>
<head>
<title>OpenLayers Example</title>
<script src="http://openlayers.org/api/OpenLayers.js"></script>
</head>
<body>
<div style="width:100%; height:100%" id="map"></div>
<script defer="defer" type="text/javascript">
var style = new OpenLayers.Style({
'externalGraphic': OpenLayers.Util.getImagesLocation() + "marker.png",
'graphicHeight': 25,
'graphicWidth': 21,
'graphicXOffset': -10.5,
'graphicYOffset': -12.5
});
var map = new OpenLayers.Map('map');
var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
var kml = new OpenLayers.Layer.Vector("KML", {strategies: OpenLayers.Strategy.Fixed(), styleMap: style, visibility: true,
protocol: new OpenLayers.Protocol.HTTP({url: "/kml/", format: new OpenLayers.Format.KML()})});
map.addLayers([wms,kml]);
map.zoomToMaxExtent();
</script>
</body>
此外,KML输出的片段,在直接加载到Google地图时有效:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<Placemark>
<name>ArSab</name>
<description>ArSab</description>
<Point><coordinates>42.615967,17.140791</coordinates></Point>
</Placemark>
<Placemark>
<name>ArBah</name>
<description>ArBah</description>
<Point><coordinates>50.534363,25.985207</coordinates></Point>
</Placemark>
<Placemark>
<name>LvBsh</name>
<description>LvBsh</description>
<Point><coordinates>35.757751,34.320755</coordinates></Point>
</Placemark>
[...]
答案 0 :(得分:0)
您是否使用firebug(在firefox中)或开发者工具(在chrome中)检查了网页网络流量?
如上所述,如果加载成功,请检查是否从您的网页收到任何错误消息。
如果控制台上没有错误消息,请检查是否有任何图层叠加层。有时它可能被其他图层覆盖,第一个加载图层通常显示在页面的最底部。
答案 1 :(得分:0)
我已经解决了这个问题。我依赖的代码(不确定从哪里确切)在小的和显然至关重要的差异上缺失: 我使用的代码是:
var kml = new OpenLayers.Layer.Vector("KML", {strategies: OpenLayers.Strategy.Fixed(), [...]
正确的版本是:
var kml = new OpenLayers.Layer.Vector("KML", {strategies: [new OpenLayers.Strategy.Fixed()], [...]
那是一个令人沮丧的下午,试图诊断问题。
顺便提一下,正如我的一条评论中所提到的,当页面正常工作时,manage.py runserver控制台会在/ map /和/ kml /上显示GET。