未捕获的TypeError:$ .ajax不是函数L.TileLayer.BetterWMS函数

时间:2018-08-20 10:03:48

标签: django leaflet geoserver

我正在尝试为我的wms显示一个onclick弹出窗口,但出现此错误:

  

未捕获的TypeError:$ .ajax不是函数。

我按照说明从以下站点下载了jquery-3.3.1.js:http://jquery.com/%20download/#jquery,并将其与L.TileLayer.BetterWMS.js一起粘贴到我的静态文件夹中,该文件位于同一目录,但我仍然看不到线层的属性。这是我的代码:

index.html

{% extends 'workorders/base.html' %}
{% block jumbotron2 %}
<div class="jumbotron">
<h1>Navbar example</h1>
<p class="lead">This example is a quick exercise to illustrate how the top-aligned navbar works. As you scroll, this navbar remains in its original position and moves with the rest of the page.</p>
<a class="btn btn-lg btn-primary" href="../../components/navbar/" role="button">View navbar docs &raquo;</a>
</div>
{% endblock %}

{% block content %}
<!DOCTYPE html>
<html>
{% load static %}
{% load leaflet_tags %}
<head>
  {% leaflet_js %}
  {% leaflet_css %}
  <title>Map</title>
  <style type="text/css">
      #gis {width: 100%;height:600px;}
  </style>
     <style type="text/css">
       #map {
        width: 100%;height:600px;
      }
    </style>
  <link rel="stylesheet" type="text/css" href="{% static 'dist/leaflet-groupedlayercontrol/leaflet.groupedlayercontrol.min.css' %}">
  <script type="text/javascript" src="{% static 'dist/leaflet.ajax.js' %}" > </script>
  <script type="text/javascript" src="{% static 'dist/leaflet-groupedlayercontrol/leaflet.groupedlayercontrol.min.js' %}" > </script>
  <script type="text/javascript" src="{% static 'L.TileLayer.BetterWMS.js' %}" > </script>
</head>
<body>

<div id="map"></div>
<script type="text/javascript" src="{% static 'jquery-3.3.1.min.js' %}"></script>

<script type="text/javascript">


      var map = L.map('map', {
        center: [42,21],
        zoom: 7
      });


      L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);

        var datasets = L.tileLayer.wms('http://localhost:8080/geoserver/geodjango/wms', {
        layers: 'geodjango:layer_ww_manholes',
        format: 'image/png',
        transparent: true
        });

      var lines = L.tileLayer.betterWms('http://localhost:8080/geoserver/geodjango/wms', {
        layers: 'geodjango:layer_ww_lines',
        transparent: true,
        format: 'image/png'
      });

        datasets.addTo(map);
        lines.addTo(map);


       var basemaps = {
          OSM: L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
          })
      };

        var groupedOverlays = {
          "Layers": {
            "ww_manholes": datasets,
            "ww_lines": lines
          }

        };

        L.control.groupedLayers(basemaps, groupedOverlays).addTo(map);   

</script>

</body>
</html>
{% endblock %}

      L.TileLayer.BetterWMS.js

L.TileLayer.BetterWMS = L.TileLayer.WMS.extend({

  onAdd: function (map) {
    // Triggered when the layer is added to a map.
    //   Register a click listener, then do all the upstream WMS things
    L.TileLayer.WMS.prototype.onAdd.call(this, map);
    map.on('click', this.getFeatureInfo, this);
  },

  onRemove: function (map) {
    // Triggered when the layer is removed from a map.
    //   Unregister a click listener, then do all the upstream WMS things
    L.TileLayer.WMS.prototype.onRemove.call(this, map);
    map.off('click', this.getFeatureInfo, this);
  },

  getFeatureInfo: function (evt) {
    // Make an AJAX request to the server and hope for the best
    var url = this.getFeatureInfoUrl(evt.latlng),
        showResults = L.Util.bind(this.showGetFeatureInfo, this);
    $.ajax({
      url: url,
      success: function (data, status, xhr) {
        var err = typeof data === 'string' ? null : data;
        showResults(err, evt.latlng, data);
      },
      error: function (xhr, status, error) {
        showResults(error);  
      }
    });
  },

  getFeatureInfoUrl: function (latlng) {
    // Construct a GetFeatureInfo request URL given a point
    var point = this._map.latLngToContainerPoint(latlng, this._map.getZoom()),
        size = this._map.getSize(),

        params = {
          request: 'GetFeatureInfo',
          service: 'WMS',
          srs: 'EPSG:4326',
          styles: this.wmsParams.styles,
          transparent: this.wmsParams.transparent,
          version: this.wmsParams.version,      
          format: this.wmsParams.format,
          bbox: this._map.getBounds().toBBoxString(),
          height: size.y,
          width: size.x,
          layers: this.wmsParams.layers,
          query_layers: this.wmsParams.layers,
          info_format: 'text/html'
        };

    params[params.version === '1.3.0' ? 'i' : 'x'] = point.x;
    params[params.version === '1.3.0' ? 'j' : 'y'] = point.y;

    return this._url + L.Util.getParamString(params, this._url, true);
  },

  showGetFeatureInfo: function (err, latlng, content) {
    if (err) { console.log(err); return; } // do nothing if there's an error

    // Otherwise show the content in a popup, or something.
    L.popup({ maxWidth: 800})
      .setLatLng(latlng)
      .setContent(content)
      .openOn(this._map);
  }
});

L.tileLayer.betterWms = function (url, options) {
  return new L.TileLayer.BetterWMS(url, options);  
};

enter image description here

1 个答案:

答案 0 :(得分:1)

  • 您必须从所有js/导入顶部的<script>文件夹中导入...

    <script type="text/javascript" src="{% static 'js/jquery-3.3.1.min.js' %}"></script>
    <script type="text/javascript" src="{% static 'js/L.TileLayer.BetterWMS.js' %}"></script>