ラベル lazy source の投稿を表示しています。 すべての投稿を表示
ラベル lazy source の投稿を表示しています。 すべての投稿を表示

2019年8月26日月曜日

ol5.3ex 7 - Lazy Source

「Lazy Source (lazy-source.html) 」を参考に地図を表示してみます。

説明に次のようにあります。

Typically, the source for a layer is provided to the layer constructor. If you need to set a layer source after construction, this can be done with the layer.setSource() method.

The layer in the map above is constructed with no source. Use the links below to set/unset the layer source. A layer is not rendered until its source is set.

一般的に、レイヤのソースはレイヤコンストラクタに提供されます。構築の後にレイヤソースを設定したい場合は、layer.setSource() メッソッドを使って実行できます。

上記マップのレイヤは、ソース無しで構築されます。レイヤソースを 設定/未設定するために下記のリンクを使います。レイヤは、ソースが設定されるまで、描画されません。


次の内容で index.js を作成します。
user@deb9-vmw:~/new-project$ vim index.js
import 'ol/ol.css';
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
import OSM from 'ol/source/OSM.js';
var source = new OSM();
/** source(option)
 * Source for this layer. 
 * レイヤのソース。(ol5 API)
 */
var layer = new TileLayer();
/** ol/layer/Tile~TileLayer 
 * For layer sources that provide pre-rendered, tiled 
 * images in grids that are organized by zoom levels for 
 * specific resolutions. 
 * プリレンダリング(事前描画)を提供するレイヤソースのため
 * の、特定の解像度でのズームレベルによって編成されているグ
 * リッドのタイルイメージ。(ol5 API)
 */
var map = new Map({
 layers: [layer],
 /** layers [Map(options)]
  * Layers. If this is not defined, a map with no layers 
  * will be rendered. Note that layers are rendered in 
  * the order supplied, so if you want, for example, a 
  * vector layer to appear on top of a tile layer, it 
  * must come after the tile layer.
  * レイヤ(layers)。これが定義されていない場合、レイヤの
  * ないマップが描画されます。レイヤは提供された順番に描画
  * されるので、例えば、ベクタレイヤがタイルレイヤの上に表
  * 示するよう望むなら、タイルレイヤの後に来なければならな
  * いことに注意してください。(ol5 API)
  */
 target: 'map',
 /** target [Map(options)]
  * The container for the map, either the element itself 
  * or the id of the element. If not specified at 
  * construction time, module:ol/Map~Map#setTarget must 
  * be called for the map to be rendered.
  * map のコンテナ、(HTML)エレメント自身、または、エレメ
  * ントの id。構築時に指定されていない場合、描画される map 
  * のために module:ol/Map~Map#setTarget が呼び出されなけ
  * ればなりません。
  * (ol5 API)
  */
 view: new View({
 /** view [Map(options)]
  * The map's view. No layer sources will be fetched 
  * unless this is specified at construction time or 
  * through module:ol/Map~Map#setView.
  * map(マップ)の view(ビュー)。これが構築時に指定され
  * ていないか、module:ol/Map~Map#setView を介さなければ、
  * 取得されるレイヤソースはありません。
  * (ol5 API)
  */
  center: [0, 0],
  /** center[new View(opt_options)]
   * The initial center for the view. The coordinate 
   * system for the center is specified with the 
   * projection option. Layer sources will not be fetched 
   * if this is not set, but the center can be set later 
   * with #setCenter.
   * view(ビュー)の初期 center(中心)値。 center の座
   * 標系は、projection(投影法)オプションで指定されます。
   * これが設定されなければレイヤソースは取得されませんが、
   * center は #setCenter で後で設定できます。
   * (ol5 API)
   */
  zoom: 2
  /** zoom[new View(opt_options)]
   * Only used if resolution is not defined. Zoom level 
   * used to calculate the initial resolution for the 
   * view. The initial resolution is determined using the 
   * #constrainResolution method.
   * resolution(解像度)が定義されていない場合のみ使用さ
   * れます。view の初期 resolution(解像度)を計算するた
   * めに使用される zoom(ズーム)レベル。初期 resolution 
   * は、#constrainResolution メソッドを使用して決定され
   * ます。(ol5 API)
   */
 })
});
document.getElementById('set-source').onclick = function() {
/** Document.getElementById()
 * The Document method getElementById() returns an 
 * Element object representing the element whose id 
 * property matches the specified string. Since element 
 * IDs are required to be unique if specified, they're a 
 * useful way to get access to a specific element 
 * quickly.
 * If you need to get access to an element which doesn't 
 * have an ID, you can use querySelector() to find the 
 * element using any selector.
 * Document の getElementById() メソッドは、 id プロパティ
 * が指定された文字列に一致する要素を表す Element オブジェ
 * クトを返します。要素の ID は、指定されていれば固有であるこ
 * とが求められているため、特定の要素にすばやくアクセスする
 * には便利な方法です。
 * ID を持たない要素にアクセスする必要がある場合は、
 * querySelector() で何らかのセレクターを使用して要素を検
 * 索することができます。
 * (MDN[https://developer.mozilla.org/ja/docs/Web/API/
 * Document/getElementById])
 */
 layer.setSource(source);
 /** setSource(source)
  * Set the layer source.
  * レイヤソースを設定します。
  * (ol5 API)
  */
};
document.getElementById('unset-source').onclick = function() {
 layer.setSource(null);
};
次に index.html を作成します。
user@deb9-vmw:~/new-project$ vim index.html
<!doctype html>
<html>
 <head>
  <title>Lazy Source</title>
  <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
  <style>
   button.code {
    font-family: Monaco,Menlo,Consolas,"Courier New",monospace;
    font-size: 12px;
    padding: 5px;
    margin: 0 5px;
   }
  </style>
 </head>
 <body>
  <div id="map" class="map"></div>
  <button id="set-source" class="code">layer.setSource(source)</button>
  <button id="unset-source" class="code">layer.setSource(null)</button>
  <script src="./index.js"></script>
 </body>
</html>
npm start を実行します。

user@deb9-vmw:~/new-project$ npm start

Webブラウザのアドレス欄に

http://localhost:1234

と入力して Enter キーを押します。

layer.setSource(source)

layer.setSource(null)


2015年4月6日月曜日

2 - ol3.4ex 103b - Lazy source example 2

「lazy-source.js(2103-ol3ex.js)」は、マップを表示するための JavaScript ファイルです。

「2103-ol3ex.js」
var source = new ol.source.MapQuest({layer: 'sat'});
 /** ol.source.MapQuest
  * Layer source for the MapQuest tile server.
  * MapQuest タイルサーバのレイヤソース。(ol3 API
  * 2 - ol3ex 23b - MapQuest example 2 参照)
  */
var layer = new ol.layer.Tile();
var map = new ol.Map({
 layers: [layer],
 renderer: exampleNS.getRendererFromQueryString(),
 // 'example-behavior.js' により URL にある renderer を返します
target: 'map',
 view: new ol.View({
  center: [0, 0],
  zoom: 2
 })
});
document.getElementById('set-source').onclick = function() {
/** GlobalEventHandlers.onclick
 * The onclick property returns the click event 
 * handler code on the current element.
 * onclick プロパティは、現在の要素の click イベント
 * ハンドラのコードを返します。
 * (MDN[https://developer.mozilla.org/en-US/
 * docs/Web/API/GlobalEventHandlers.onclick])
 */
 layer.setSource(source);
};
document.getElementById('unset-source').onclick = function() {
 layer.setSource(null);
};


2 - ol3.4ex 103a - Lazy source example 1

「Lazy source example (lazy-source.html)」を参考に地図を表示してみます。
説明に次のようにあります。

Example of setting a layer source after construction.
Typically, the source for a layer is provided to the layer constructor. If you need to set a layer source after construction, this can be done with the layer.setSource() method.
The layer in the map above is constructed with no source. Use the links below to set/unset the layer source. A layer is not rendered until its source is set.

構築後のレイヤのソースを設定する例。
一般的に、レイヤのためのソースは、レイヤコンストラクタに提供されます。構築後レイヤのソースを設定する必要がある場合、layer.setSource()メソッドを用いて実行できます。
上記マップのレイヤは、ソースなしで構築されています。レイヤーソースを、設定/未設定するには、下記リンクを使用します。レイヤはそのソースが設定されるまでレンダリングされません。


HTML ファイルの作成
a Eclipse のメニューの「ファイル」->「ファイルを開く」をクリックします。





b 「ファイルを開く」ウィンドウで、「user」->「mapsite」->「ol3proj」->「v3.4.0」->「examples」->「lazy-source.html」をクリックして選択し、「OK」ボタンをクリックします。
同じように「lazy-source.js」を開きます。





c メニューの「ファイル」->「新規」 -> 「ファイル」をクリックします。



d 「ファイル」ウィンドウで「ol3proj」をクリックして選択し、「ファイル名」を「2103-ol3ex.html」と入力し、「次へ」ボタンをクリックします。








e 「File Template」ウィンドウで「HTML 5 Template」をクリックして選択し、「OK」ボタンをクリックします。











f 「lazy-source.html」の内容をコピーして「2103-ol3ex.html」に貼り付け、修正します。
g 同じように、新規に「2103-ol3ex.js」ファイルを作成し、「File Template」ウィンドウで「JavaScript Template」をクリックして選択し、「完了」ボタンをクリックして、「lazy-source.js」の内容をコピーして貼り付け、修正します。「lazy-source-require.js」も「2103-ol3ex-require.js」に貼り付けます。

「2103-ol3ex.html」
<!doctype html>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="chrome=1">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  <!--
  <link rel="stylesheet" href="../css/ol.css" type="text/css">
  <link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
  <link rel="stylesheet" href="../resources/layout.css" type="text/css">
  <link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
  -->
  <!-- ディレクトリ修正 -->
  <link rel="stylesheet" href="v3.4.0/css/ol.css" type="text/css">
  <link rel="stylesheet" href="v3.4.0/resources/bootstrap/css/bootstrap.min.css" type="text/css">
  <link rel="stylesheet" href="v3.4.0/resources/layout.css" type="text/css">
  <link rel="stylesheet" href="v3.4.0/resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
  <title>Image load events example</title>
  <style>
   button.code {
    font-family: Monaco,Menlo,Consolas,"Courier New",monospace;
    font-size: 12px;
    padding: 5px;
    margin: 0 5px;
   }
  </style>
 </head>
 <body>
  <!-- 
  bootstrap.min.css, bootstrap-responsive.min.css 
   Examples用 CSSファイルで設定されたセレクタを使用。
  -->
  <div class="navbar navbar-inverse navbar-fixed-top">
   <div class="navbar-inner">
    <div class="container">
     <!--
     <a class="brand" href="./"><img src="../resources/logo.png"> OpenLayers 3 Examples</a>
     -->
     <!-- ディレクトリ修正 -->
     <a class="brand" href="v3.4.0/examples/"><img src="v3.4.0/resources/logo.png"> OpenLayers 3 Examples</a>
    </div>
   </div>
  </div>
  <div class="container-fluid">
   <div class="row-fluid">
    <div class="span12">
     <div id="map" class="map"></div>
    </div>
   </div>
   <div class="row-fluid">
    <div class="span12">
     <h4 id="title">Lazy source example</h4>
     <p id="shortdesc">Example of setting a layer source 
     after construction.</p>
     <div id="docs">
      <p>Typically, the source for a layer is provided to the 
       layer constructor. If you need to set a layer source 
       after construction, this can be done with the 
       <code>layer.setSource()</code> method.</p>
      <p>The layer in the map above is constructed with no 
       source. Use the links below to set/unset the layer 
       source. A layer is not rendered until its source is 
       set. </p>
      <p>
       <button id="set-source" class="code">layer.setSource(source)</button>
       <button id="unset-source" class="code">layer.setSource(null)</button>
      </p>
      <!--
      <p>See the <a href="lazy-source.js" target="_blank">lazy-source.js source</a> for more detail on how this is done.</p>
      -->
      <!-- ファイル修正 -->
      <p>See the <a href="2103-ol3ex.js" target="_blank">2103-ol3ex.js source</a> for more detail on how this is done.</p>
     </div>
     <div id="tags">source</div>
    </div>
   </div>
  </div>
  <!--
  <script src="../resources/jquery.min.js" type="text/javascript"></script>
  <script src="../resources/example-behaviour.js" type="text/javascript"></script>
  -->
  <!-- ディレクトリ修正
   jQuery Minified版と
   example-behaviour.js(Examples用 JSコード[文字コードなど])
 -->
  <script src="v3.4.0/resources/jquery.min.js" type="text/javascript"></script>
  <script src="v3.4.0/resources/example-behaviour.js" type="text/javascript"></script>
  <!--
  <script src="loader.js?id=lazy-source" type="text/javascript"></script>
  -->
  <!-- ファイル修正 -->  <!-- ディレクトリ修正 -->
  <script src="loader.js?id=2103-ol3ex" type="text/javascript"></script>
  </body>
</html>