Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django和OpenLayers:不显示KML数据_Django_Maps_Openlayers_Geodjango - Fatal编程技术网

Django和OpenLayers:不显示KML数据

Django和OpenLayers:不显示KML数据,django,maps,openlayers,geodjango,Django,Maps,Openlayers,Geodjango,我是Django/OpenLayers的新手,我一直在学习各种教程,尤其是。我使用一个调用内置GeoDjango模板的视图来动态呈现KML,我尝试将其传递到一个页面以使用OpenLayers呈现地图。但是,我得到的不是显示KML文件中所有点的地图,而是默认地图,就像我从未在map.html文件中添加“KML”层一样。我怀疑KML文件没有加载到OpenLayers中,但我不明白为什么不加载,或者如何测试KML数据是否加载成功。或者,KML正在成功加载,但由于某些原因无法正确显示 my models

我是Django/OpenLayers的新手,我一直在学习各种教程,尤其是。我使用一个调用内置GeoDjango模板的视图来动态呈现KML,我尝试将其传递到一个页面以使用OpenLayers呈现地图。但是,我得到的不是显示KML文件中所有点的地图,而是默认地图,就像我从未在map.html文件中添加“KML”层一样。我怀疑KML文件没有加载到OpenLayers中,但我不明白为什么不加载,或者如何测试KML数据是否加载成功。或者,KML正在成功加载,但由于某些原因无法正确显示

my 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
My 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})
简单URL.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>

OpenLayers示例
var style=新的OpenLayers.style({
“externalGraphic”:OpenLayers.Util.getImagesLocation()+“marker.png”,
“图形高度”:25,
“图形宽度”:21,
“graphicXOffset”:-10.5,
“图形偏移量”:-12.5
});
var map=newopenlayers.map('map');
var wms=new OpenLayers.Layer.wms(“OpenLayers wms”,
"http://vmap0.tiles.osgeo.org/wms/vmap0“,{层:'基本'});
var kml=new OpenLayers.Layer.Vector(“kml”),{strategies:OpenLayers.Strategy.Fixed(),styleMap:style,visibility:true,
协议:new OpenLayers.protocol.HTTP({url://kml/”,格式:new OpenLayers.format.kml()});
map.addLayers([wms,kml]);
zoomToMaxExtent();
另外,KML输出的一个片段,当直接加载到Google Maps时可以工作:

<?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>
[...]

阿尔萨布
阿尔萨布
42.615967,17.140791
阿尔巴
阿尔巴
50.534363,25.985207
LvBsh
LvBsh
35.757751,34.320755
[...]

您是否使用firebug(在firefox中)或开发者工具(在chrome中)检查过您的网页网络流量

如上所述,如果加载成功,请检查是否从您的网页收到任何错误消息


如果控制台上没有错误消息,则检查是否有任何图层覆盖。有时它可能被其他层覆盖,第一个加载层通常显示在页面的最底部。

我已经解决了这个问题。我所依赖的代码(不确定具体从何处来)在微小的、显然至关重要的差异上缺失了: 我使用的代码是:

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。如果没有错误,这很可能与投影有关。也就是说,它正在加载,但是你看不到任何东西,因为你的kml是以度为单位的,而你的地图是以米为单位的。从他的代码来看,他似乎使用了默认的投影EPSG 4326,从kml文件来看,那里的投影似乎也是4326,所以我真的想用一些方法来检查图层是否确实接收和加载了kml数据。无论我给出的URL是什么(/foobar/不存在,等等),我都会得到相同的结果-没有错误消息,什么都没有。有没有办法检查它是否成功获取KML数据?另一个测试:我将map.html页面与arabicpoints.KML文件一起放入另一个文件夹。我将“protocol:…”行更改为“protocol:new OpenLayers.protocol.HTTP({url:“arabicpoints.kml”,format:new OpenLayers.format.kml()})”,但它似乎仍然没有显示kml文件中的数据点。我也尝试过使用绝对url(file:///D:/...),又一次没有运气。你确定它来自KML吗?我的意思是,你看到的地图只有WMS层吗?我从网页上得到的唯一错误是一个CSS错误,我认为是由导入的OpenLayers javascript(或者可能来自一些firefox扩展)引发的:样式表无法加载。这甚至似乎在重新加载时消失了。我不知道如何检查图层覆盖。如果我将{layers:'basic'}添加到KML层并仅加载该层,我只会得到一个“TypeError:this.baseLayer is null”,我不完全确定你所说的网页流量是什么意思。我在manage.py的runserver提供的本地服务器上运行这个。但是,我注意到命令行显示了一个页面的服务时间——假设map.html页面也获得了/kml/page,那么runserver命令将输出如下内容:[DATE TIME]“GET/map/HTTP/1.1”[DATE TIME]“GET/kml/HTTP/1.1”,因为它没有输出GET to/kml/,这是否意味着传递给OpenLayers.Protocol.HTTP的URL的语法有问题?