无法使用传单在地图上绘制形状

我正在使用 javascript 库传单来处理地图,我现在做了位置搜索部分,我希望用户可以在地图上绘制圆、线和多边形,但无法正常工作。搜索工具栏可见,但形状不可见。任何人都可以指出我的代码中的错误,在此先感谢。这是我的代码:

<!DOCTYPE html>

<html>


<head>

  <!-- Load Leaflet from CDN-->

  <link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />

  <script src="https://unpkg.com/leaflet/dist/leaflet-src.js"></script>


  <!-- Load Esri Leaflet from CDN -->

  <script src="https://unpkg.com/esri-leaflet"></script>


  <!-- Esri Leaflet Geocoder -->

  <link rel="stylesheet" href="https://unpkg.com/esri-leaflet-geocoder/dist/esri-leaflet-geocoder.css">

  <script src="https://unpkg.com/esri-leaflet-geocoder"></script>

  <script src="node_modules/leaflet-toolbar/dist/leaflet.toolbar.js"></script>


  <link rel="stylesheet" href="node_modules/leaflet-toolbar/dist/leaflet.toolbar.css" />

  <link rel="stylesheet" href="node_modules/leaflet-draw-toolbar/dist/leaflet.draw-toolbar.css" />

  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

  <!----    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

    <script src="../node_modules/leaflet/dist/leaflet-src.js"></script>

    <script src="../node_modules/leaflet-toolbar/dist/leaflet.toolbar-src.js"></script>

    <script src="../node_modules/leaflet-draw/dist/leaflet.draw-src.js"></script>--!-->

  <style>



繁星淼淼
浏览 146回答 1
1回答

慕桂英546537

使用错误var results = L.layerGroup.addTo(map);应该var results = new L.LayerGroup().addTo(map);(就像您在搜索容器中所做的那样)<!DOCTYPE html><html><head>&nbsp; <!-- Load Leaflet from CDN-->&nbsp; <link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />&nbsp; <script src="https://unpkg.com/leaflet/dist/leaflet-src.js"></script>&nbsp; <!-- Load Esri Leaflet from CDN -->&nbsp; <script src="https://unpkg.com/esri-leaflet"></script>&nbsp; <!-- Esri Leaflet Geocoder -->&nbsp; <link rel="stylesheet" href="https://unpkg.com/esri-leaflet-geocoder/dist/esri-leaflet-geocoder.css">&nbsp; <script src="https://unpkg.com/esri-leaflet-geocoder"></script>&nbsp; <script src="node_modules/leaflet-toolbar/dist/leaflet.toolbar.js"></script>&nbsp; <link rel="stylesheet" href="node_modules/leaflet-toolbar/dist/leaflet.toolbar.css" />&nbsp; <link rel="stylesheet" href="node_modules/leaflet-draw-toolbar/dist/leaflet.draw-toolbar.css" />&nbsp; <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>&nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>&nbsp; <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>&nbsp; <!----&nbsp; &nbsp; <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>&nbsp; &nbsp; <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>&nbsp; &nbsp; <script src="../node_modules/leaflet/dist/leaflet-src.js"></script>&nbsp; &nbsp; <script src="../node_modules/leaflet-toolbar/dist/leaflet.toolbar-src.js"></script>&nbsp; &nbsp; <script src="../node_modules/leaflet-draw/dist/leaflet.draw-src.js"></script>--!-->&nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>&nbsp; <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.css" />&nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js"></script>&nbsp; <script src="https://cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js"></script>&nbsp; <style>&nbsp; &nbsp; #map {&nbsp; &nbsp; &nbsp; position: absolute;&nbsp; &nbsp; &nbsp; top: 0;&nbsp; &nbsp; &nbsp; bottom: 0;&nbsp; &nbsp; &nbsp; left: 0;&nbsp; &nbsp; &nbsp; right: 0;&nbsp; &nbsp; }&nbsp; </style></head><body>&nbsp; <div id="map"> </div>&nbsp; <script>&nbsp; &nbsp; var center = [-33.8650, 151.2094];&nbsp; &nbsp; var map = L.map('map').setView([0, 0], 6);&nbsp; &nbsp; drawnItems = new L.FeatureGroup().addTo(map);&nbsp; &nbsp; L.tileLayer('https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=eL1sdTPWF7XeyxpLvpGq', {&nbsp; &nbsp; &nbsp; attribution: '<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>'&nbsp; &nbsp; }).addTo(map);&nbsp; &nbsp; var searchContrl = L.esri.Geocoding.geosearch().addTo(map);&nbsp; &nbsp; //adding layergroup to search control&nbsp; &nbsp; console.log(L.LayerGroup);&nbsp; &nbsp; var results = new L.LayerGroup().addTo(map);&nbsp; &nbsp; searchContrl.on('results', function(data) {&nbsp; &nbsp; &nbsp; results.clearLayers();&nbsp; &nbsp; &nbsp; for (var i = data.results.length - 1; i >= 0; i--) {&nbsp; &nbsp; &nbsp; &nbsp; results.addLayer(L.marker(data.results[i].latlong));&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; var editableLayers = new L.FeatureGroup();&nbsp; &nbsp; map.addLayer(editableLayers);&nbsp; &nbsp; var MyCustomMarker = L.Icon.extend({&nbsp; &nbsp; &nbsp; options: {&nbsp; &nbsp; &nbsp; &nbsp; shadowUrl: null,&nbsp; &nbsp; &nbsp; &nbsp; iconAnchor: new L.Point(12, 12),&nbsp; &nbsp; &nbsp; &nbsp; iconSize: new L.Point(24, 24),&nbsp; &nbsp; &nbsp; &nbsp; iconUrl: 'link/to/image.png'&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; var options = {&nbsp; &nbsp; &nbsp; position: 'topright',&nbsp; &nbsp; &nbsp; draw: {&nbsp; &nbsp; &nbsp; &nbsp; polyline: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shapeOptions: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: '#f357a1',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; weight: 10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; polygon: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allowIntersection: false, // Restricts shapes to simple polygons&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawError: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: '#e1e100', // Color the shape will turn when intersects&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message: '<strong>Oh snap!<strong> you can\'t draw that!' // Message that will show when intersect&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shapeOptions: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: '#bada55'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; circle: false, // Turns off this drawing tool&nbsp; &nbsp; &nbsp; &nbsp; rectangle: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shapeOptions: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clickable: false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; marker: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; icon: new MyCustomMarker()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; edit: {&nbsp; &nbsp; &nbsp; &nbsp; featureGroup: editableLayers, //REQUIRED!!&nbsp; &nbsp; &nbsp; &nbsp; remove: false&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; var drawControl = new L.Control.Draw(options);&nbsp; &nbsp; map.addControl(drawControl);&nbsp; &nbsp; map.on(L.Draw.Event.CREATED, function(e) {&nbsp; &nbsp; &nbsp; var type = e.layerType,&nbsp; &nbsp; &nbsp; &nbsp; layer = e.layer;&nbsp; &nbsp; &nbsp; if (type === 'marker') {&nbsp; &nbsp; &nbsp; &nbsp; layer.bindPopup('A popup!');&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; editableLayers.addLayer(layer);&nbsp; &nbsp; });&nbsp; </script></body></html>顺便说一句,我使用var了因为你的语法,但const更好let。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript