2017-09-29 1 views
0

GIS Stack Exchangeでこの質問をしましたが、誰も触れていません。おそらく、これはGISよりも純粋なjavascriptの問題だったかもしれません。リファレンスエラー:モジュールが定義されていません(gitHubで見つかったlasso selectコード)

私はWebマッピングのnubeで、ユーザーが選択のために機能を追加する機能をWebマップに追加しようとしています。

<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Residential Garbage - Monday</title> 
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet.css" type="text/css"> 
    <script src="http://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet-src.js" crossorigin=""></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    <link rel="stylesheet" href="style2.css" type="text/css"> 
    <script src="/CO_054/JS/utils.js"></script> 
    <script src="/CO_054/JS/index.js"></script> 


    <script type="text/javascript"> 
    var map; 
    function init() { 
     map = new L.map('map'); 
     map.setView([37.396,-122.102],14.57); 

     // Add the tiled layer    
     var tiles = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {attribution: "Data copyright OpenStreetMap contributors"}); 
     tiles.addTo(map); 

     var mondayLayer = L.tileLayer.wms('http://recolrr01.norcalwaste.com:8080/geoserver/CO_054/wms', { 
      layers: 'CO_054:residential_garbage_monday', 
      format: 'image/png', 
      transparent: true 
     }); 

     mondayLayer.addTo(map); 

     // define event handler function for click events and register it 

      function Identify(e) 
      { 
      // set parameters needed for GetFeatureInfo WMS request 
      var sw = map.options.crs.project(map.getBounds().getSouthWest()); 
      var ne = map.options.crs.project(map.getBounds().getNorthEast()); 
      var BBOX = sw.x + "," + sw.y + "," + ne.x + "," + ne.y; 
      var WIDTH = map.getSize().x; 
      var HEIGHT = map.getSize().y; 

      var X = Math.trunc(map.layerPointToContainerPoint(e.layerPoint).x); 
      var Y = Math.trunc(map.layerPointToContainerPoint(e.layerPoint).y); 

      // compose the URL for the request 
      var URL = 'http://recolrr01.norcalwaste.com:8080/geoserver/CO_054/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetFeatureInfo&LAYERS=CO_054:residential_garbage_monday&QUERY_LAYERS=CO_054:residential_garbage_monday&BBOX='+BBOX+'&FEATURE_COUNT=1&HEIGHT='+HEIGHT+'&WIDTH='+WIDTH+'&INFO_FORMAT=application%2Fjson&TILED=false&CRS=EPSG%3A3857&I='+X+'&J='+Y; 

      //send GetFeatureInfo as asynchronous HTTP request using jQuery $.ajax 

      $.ajax({ 
       url: URL, 
       dataType: "json", 
       type: "GET", 
       success: function(data) 
       { 
       if(data.features.length !== 0) { // at least one feature returned in response 
        var returnedFeature = data.features[0]; // first feature from response 

        // Set up popup for clicked feature and open it 
        var popup = new L.Popup({ 
        maxWidth: 300 
        }); 

        $('#address-details').html("<b>" + returnedFeature.properties.Address + "</b><br><b>Customer Name:</b> " + returnedFeature.properties.Customer_N + "<br><b>Customer Route:</b> " + returnedFeature.properties.Exist_Rout + "<br><b>Customer Tons:</b> " + returnedFeature.properties.Demand + "<br><b>Container Size:</b>" + returnedFeature.properties.Z1SIZE + "<br><b>Account Number:</b> " + returnedFeature.properties.Z1SVC_); 
        } 
       } 
      }); 
      } 

      map.addEventListener('click', Identify); 

      const lasso = L.lassoSelect({ activeTooltip }).addTo(map); 

      lasso.on('pathchange',() => { 
       // get selected path (an array of LatLng positions) 
       const path = lasso.getPath(); 

       // or check if a point is inside the selected path 
       if (this.lasso.contains(someMarker.getLatLng())) { 
       // ... 
       } 

      }); 

      lasso.enable(); 
} 



</script> 
    </head> 
    <body onload="init()"> 
    <h1 id="title">Mountain View - Residential Garbage - Monday</h1> 
    <div id="map" class="smallmap"></div> 
    <div id='address-details'>   </div> 
    <div id="summaryLabel"> 
     <p>Click a service location on the map to get more information.</p> 
     <p class="legendRed">02X </p><p class="legendGreen">03X </p><p class="legendBeige">04X </p><p class="legendBlue">05X</p> 
    </div> 
    </body> 
    </html> 

<style> 
.legendRed { 
    color: #ff0000; 
} 
.legendGreen { 
    color: #33a02c; 
} 
.legendBeige { 
    color: #fdbf6f; 
} 
.legendBlue { 
    color: #1f78b4; 
} 
    #map { 
    border: 1px solid #ff0000; 
    float: left; 
    } 
    #address-details { 
    border: 1px solid #00ff00; 
    float: right; 
    width: 190px; 
    height: 100%; 

    } 
    #summaryLabel { 
    clear: both; 
    } 
</style> 

そして、ここでコンソールでのエラーです:

12:15:42.775 ReferenceError: module is not defined 1 utils.js:1:1 
    <anonymous> http://recolrr01.norcalwaste.com:8080/CO_054/JS/utils.js:1:1 
であるとしてここで

https://github.com/ImperialCollegeLondon/leaflet-lassoselect

が私のコードされています。これを行うには、私はgithubの上で見つかったこのコードを使用しています

ここにutils.jsコードがあります。

module.exports.contains = function(path, point) { 
    // ray-casting algorithm based on 
    // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html 
    var x = point.lat, y = point.lng; 
    var inside = false; 
    for (var i = 0, j = path.length - 1; i < path.length; j = i++) { 
     var xi = path[i].lat, yi = path[i].lng; 
     var xj = path[j].lat, yj = path[j].lng; 
     var intersect = ((yi > y) != (yj > y)) && 
         (x < (xj - xi) * (y - yi)/(yj - yi) + xi); 
     if (intersect) { 
     inside = !inside; 
     } 
    } 
    return inside; 
}; 
READMEファイルで0

は、命令のような、次のとおりです。

Install the plug-in: 

npm install github:imperialcollegelondon/leaflet-lassoselect 

    Import the plug-in 

import 'leaflet-lassoselect'; 

しかし、私は、限定されたサーバにアクセスし、CMDプロンプトに取得する方法はありませんとシンクライアントの午前。

このコードを動作させるために何かできることはありますか、それともプラグインのインストールを必要としない別のバージョンがありますか?

+0

明らかかもしれませんが、どのように初期化するのですか。 'module = {}; module.exports = {} ' ' utils.js'の最初のものは? – user10089632

+0

@ user10089632それはエラーを削除しましたが、正しい動作を引き起こしているとは思われません。私は、 "npm install github:imperialcollegelondon/leaflet-lassoselect"を使って参照する必要がある方法があるかどうかを調べようとしています。 – ShaunO

+0

絶対に、私はあなたが 'モジュール'の使用法について研究し、以前の摂取を使用しないことをお勧めします。E全体のロジックには影響しません) – user10089632

答えて

0

プラグインからファイルutils.jsを含めないでください。全く必要とは思われません。

あなたはmodulemodule.exportsが表示されたら、それはそれはおそらくの一部である場合には、これらの変数の可用性のためのコードを最初にチェックし、しない限り、ファイルはCommonJS モジュールローダによってロードされることが予想されることを意味UMDラッパーであり、直接ブラウザーの消費に適している可能性があります(つまり、<script>タグを含める)。

コードがnpmを通じて利用可能とimport「EDまたはrequire D」であることが予想され

は、プロジェクトのルートでご利用いただけます package.jsonファイルを持っています。このファイルでは、 "main"キーを探します。インポート時に実際に読み込まれるファイルを教えてくれます。そのようなキーがない場合、デフォルトでは、ローダーは index.jsファイルがルートで使用可能になると予想します。

さらに、ノード、npm、モジュールローダーまたはビルドエンジンを使用して学ぶ微妙な要素があります。

+0

@ ghybs私はutils.jsを取り除きましたが、const lasso = L.lassoSelect({activeTooltip})を宣言する行にエラーが発生しています。これはReferenceErrorを与えています:activeTooltipは定義されていません – ShaunO

+0

まったく別の問題のようなものです。新しい質問を開いてください。新しい質問で、あなたのコードをどのように思いついたか説明してください。 – ghybs

-1

<script>タグを閉じるタグ<body>の直前にHTMLの下部に移動します。

+0

@ Ron Royston私はまだモジュールが定義されていない取得しています。また、ここでエラーが発生します:const lasso = L.lassoSelect({activeTooltip})。addTo(map); 15:45:29.832 ReferenceError:activeTooltipが定義されていません1 ResGarbMonday134.html:85:46 \t init http://recolrr01.norcalwaste.com:8080/CO_054/ResGarbMonday134.html:85:46 \t onload http:// recolrr01.norcalwaste.com:8080/CO_054/ResGarbMonday134.html:1:1 – ShaunO

+0

map/mapScriptsが読み込まれると、Googleマップがコールバックを実行する方法がわかります。コールバックを使うのか、それとも新しい 'promise'メソッドを使うのかにかかわらず、スクリプトを実行する前に読み込まれたものの非同期性を扱わなければなりません。 –

関連する問題