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

2014年9月29日月曜日

2 - ol3ex 9b - Vector layer example 2

「vector-layer.js(209-ol3ex.js)」は、地図を表示するのに必要な javascript です。
「countries.geojson」データの内容は次のようになっていました。
「countries.geojson」
{"type":"FeatureCollection",
 "features":[{
  "type":"Feature",
  "id":"AFG",
  "properties":{"name":"Afghanistan"},
  "geometry":{
   "type":"Polygon",
   "coordinates":[[[61.210817,35.650072],...

「210-ol3ex.js」
var styleCache = {};
var vectorLayer = new ol.layer.Vector({
/** ol.layer.Vector 
 * Vector data that is rendered client-side. Note that any 
 * property set in the options is set as a ol.Object property
 * on the layer object; for example, setting title: 'My 
 * Title' in the options means that title is observable, and 
 * has get/set accessors.
 * クライアント側で描画されたベクタデータ。オプションで設定した任
 * 意のプロパティは、レイヤオブジェクトで ol.Object プロパティ
 * として設定されていることに注意してください。たとえば、オプショ
 * ンで、title:'My Title' を設定することは、タイトルは 
 * observable で、アクセサを取得/設定することを意味します。
 * (ol3 API)
 */
 source: new ol.source.GeoJSON({
 /** ol.source.GeoJSON 
  * Static vector source in GeoJSON format
  * GeoJSON フォーマットの静的ベクタソース。(ol3 API)
  */
  projection: 'EPSG:3857',
//url: 'data/geojson/countries.geojson',
  url: 'v3.0.0/examples/data/geojson/countries.geojson',
 }),
 style: function(feature, resolution) {
  var text = resolution < 5000 ? feature.get('name') : '';
 /** 条件演算子 condition ? expr1 : expr2 
   * condition: true か false かを評価する条件文です。
   * expr1, expr2: 各々の値の場合に実行する式です。
   * condition が true の場合、演算子は expr1 の値を選択します。
   * そうでない場合は expr2 の値を選択します。
   * (MDN[https://developer.mozilla.org/ja/docs/Web/
   * JavaScript/Guide/Expressions_and_Operators])
   */
  // 解像度が 5000 以下なら ラベル(name 国名)非表示
  if (!styleCache[text]) {
  // 'styleCache[text]={}' なので 'if(true)'
   styleCache[text] = [new ol.style.Style({
   /** ol.style.Style 
    * Base class for vector feature rendering styles.
    * ベクタフィーチャがスタイルを描画するための基本クラス。
    * (ol3 API)
    */
    fill: new ol.style.Fill({
    /** ol.style.Fill 
     * Set fill style for vector features.
     * ベクタフィーチャの塗りつぶしスタイルを設定。(ol3 API)
     */
     color: 'rgba(255, 255, 255, 0.6)'
    }),
    stroke: new ol.style.Stroke({
    /** ol.style.Stroke 
     * Set stroke style for vector features. 
     * Note that the defaults given are the Canvas defaults, 
     * which will be used if option is not defined. 
     * The get functions return whatever was entered in the 
     * options; they will not return the default.
     * ベクタフィーチャのためのストロークスタイルの設定。
     * デフォルトは、オプションが定義されていない場合に使用され
     * る Canvas のデフォルトを与えられることに注意してくださ 
     * い。GET関数は、オプションで入力されたものはすべて返しま
     * す。それらはデフォルトを返しません。(ol3 API)
     */
     color: '#319FD3',
     width: 1
    }),
    text: new ol.style.Text({
    /** ol.style.Text
     * Set text style for vector features.
     * ベクタフィーチャの文字列のスタイルを設定します。
     * (ol3 API)
     */
     font: '12px Calibri,sans-serif',
     text: text,
     fill: new ol.style.Fill({
      color: '#000'
     }),
     stroke: new ol.style.Stroke({
      color: '#fff',
      width: 3
     })
    })
   })];
  }
  return styleCache[text];
 }
});
var map = new ol.Map({
 layers: [
  new ol.layer.Tile({
  /** ol.layer.Tile 
   * For layer sources that provide pre-rendered, tiled 
   * images in grids that are organized by zoom levels for 
   * specific resolutions. 
   * プリレンダリング(事前描画)を提供するレイヤソースのための、
   * 特定の解像度でのズームレベルによって編成されているグリッドの
   * タイルイメージ。(ol3 API)
   */
   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 参照)
    */
  }),
  vectorLayer
 ],
 target: 'map',
 view: new ol.View({
  center: [0, 0],
  zoom: 1
 })
});
var highlightStyleCache = {};
var featureOverlay = new ol.FeatureOverlay({
/** ol.FeatureOverlay
 * A mechanism for changing the style of a small number of 
 * features on a temporary basis, for example highlighting. 
 * This is necessary with the Canvas renderer, where, unlike
 * in SVG, features cannot be individually referenced.
 * ハイライトのように、一時的に少数のフィーチャがスタイルの変更す
 * るためのメカニズム。これは Canvas レンダラが必要で、SVGとは異
 * なり、フィーチャを個別に参照することはできません。(ol3 API)
 */
 map: map,
 style: function(feature, resolution) {
  var text = resolution < 5000 ? feature.get('name') : '';
  if (!highlightStyleCache[text]) {
   highlightStyleCache[text] = [new ol.style.Style({
    stroke: new ol.style.Stroke({
     color: '#f00',
     width: 1
    }),
    fill: new ol.style.Fill({
     color: 'rgba(255,0,0,0.1)'
    }),
    text: new ol.style.Text({
     font: '12px Calibri,sans-serif',
     text: text,
     fill: new ol.style.Fill({
      color: '#000'
     }),
     stroke: new ol.style.Stroke({
      color: '#f00',
      width: 3
     })
    })
   })];
  }
  return highlightStyleCache[text];
 }
});
var hightlight;
var displayFeatureInfo = function(pixel) {
 var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
 /** forEachFeatureAtPixel()
  * Detect features that intersect a pixel on the viewport, 
  * and execute a callback with each intersecting feature. 
  * Layers included in the detection can be configured 
  * through opt_layerFilter. Feature overlays will always 
  * be included in the detection.
  * ビューポート上のピクセルと交差するフィーチャを検出し、
  * 各交差するフィーチャとコールバックを実行します。
  * 検出に含まれるレイヤが opt_layerFilter を通じて
  * 設定することができます。フィーチャーオーバーレイは
  * 常に検出に含まれます。(ol3 API)
  */
  return feature;
 });
 // 国名(name)の表示 
 var info = document.getElementById('info');
 if (feature) {
  info.innerHTML = feature.getId() + ': ' + feature.get('name');
  /** getId()
   * Return: id(ol3 API)
   */
 } else {
  info.innerHTML = '&nbsp;';
 }
 // フィーチャのハイライトのオンオフ
 if (feature !== highlight) {
  if (highlight) {
   featureOverlay.removeFeature(highlight);
   /** removeFeature(feature)
    * Name: feature, Type: ol.Feature, 
    * Description: Feature (ol3 API)
    */
  }
  if (feature) {
   featureOverlay.addFeature(feature);
   /** addFeature(feature)
    * Name: feature, Type: ol.Feature, 
    * Description: Feature (ol3 API)
    */
  }
   highlight = feature;
 }
};
/** マウス(ポインタ)が動いた時、ビューポートのピクセル位置を
 * 取得して displayFeatureInfo を実行。
 */
$(map.getViewport()).on('mousemove', function(evt) {
/** getViewport()
 * Return: Viewport (ol3 API)
 */
/** jQuery on イベント */
 var pixel = map.getEventPixel(evt.originalEvent);
 displayFeatureInfo(pixel);
 /** getEventPixel()
  * Returns the map pixel position for a browser event.
  * ブラウザイベントに対して、マップのピクセル位置を返します。
  * (ol3 API)
  */
});
// マウスクリック時?
map.on('click', function(evt) {
/** on
 * Listen for a certain type of event.
 * あるタイプのイベントをリッスンします。(ol3 API)
 */
 displayFeatureInfo(evt.pixel);
});


originalEvent
「ol.MapBrowserEvent」
(Events emitted as map browser events are instances of this type. See ol.Map for which events trigger a map browser event.
マップブラウザイベントとして発するイベントは、このタイプのインスタンスです。イベントがマップブラウザイベントをトリガする ol.Map を参照してください。)
のメンバ。

ol/ol/mapbrowserevent.js 32行目
 * @param {goog.events.BrowserEvent} browserEvent Browser event.
ol/ol/mapbrowserevent.js 52行目
 this.originalEvent = browserEvent.getBrowserEvent();

goog.events.BrowserEvent
「events.BrowserEvent - Extends goog.events.Event」
(http://docs.closure-library.googlecode.com/git/class_goog_events_BrowserEvent.html)
Accepts a browser event object and creates a patched, cross browser event object. The content of this object will not be initialized if no event object is provided. If this is the case, init() needs to be invoked separately.
events.BrowserEvent。goog.events.Eventを拡張します。
ブラウザイベントオブジェクトを受け入れ、パッチを適用した、クロスブラウザイベントオブジェクトを作成します。イベントオブジェクトが提供されない場合は、このオブジェクトの内容は初期化されません。この場合は、init()は別々に呼び出す必要があります。

「Instance Methods」
getBrowserEvent() ⇒ Event
No description.
Returns: Event  The underlying browser event object.

2 - ol3ex 9a - Vector layer example 1

「Vector layer example(vector-layer.html)」を参考に地図を表示してみます。HTML ファイルの作成
a Eclipse のメニューの「ファイル」->「ファイルを開く」をクリックします。





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





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



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








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











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


「209-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.0.0/css/ol.css" type="text/css">
  <link rel="stylesheet" href="v3.0.0/resources/bootstrap/css/bootstrap.min.css" type="text/css">
  <link rel="stylesheet" href="v3.0.0/resources/layout.css" type="text/css">
  <link rel="stylesheet" href="v3.0.0/resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
  <title>Vector layer example</title>
 </head>
 <body>

  <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.0.0/examples/"><img src="v3.0.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="span4">
     <h4 id="title">Vector layer example</h4>
     <p id="shortdesc">Example of a countries vector layer with country information on hover and country labels at higher zoom levels.</p>
     <div id="docs">
<!--
      <p>See the <a href="vector-layer.js" target="_blank">vector-layer.js source</a> to see how this is done.</p>
-->
       <!-- ファイル修正 -->
      <p>See the <a href="209-ol3ex.js" target="_blank">209-ol3ex.js source</a> to see on how this is done.</p>
     </div>
     <div id="tags">vector, geojson, style, feature overlay</div>
    </div>
    <div class="span4 offset4">
     <div id="info" class="alert alert-success">
       
     </div>
    </div>
   </div>
  </div>
<!--
  <script src="jquery.min.js" type="text/javascript"></script>
  <script src="../resources/example-behaviour.js" type="text/javascript"></script>
-->
  <!-- ディレクトリ修正 -->
  <script src="v3.0.0/examples/jquery.min.js" type="text/javascript"></script>
  <script src="v3.0.0/resources/example-behaviour.js" type="text/javascript"></script>
<!--
  <script src="loader.js?id=vector-layer" type="text/javascript"></script>
-->
  <!-- ファイル修正 -->  <!-- ディレクトリ修正 -->
  <script src="loader.js?id=209-ol3ex" type="text/javascript"></script>
 </body>
</html>

2014年7月3日木曜日

2 - ol3-beta.5ex 10b - Vector layer example 2

「vector-layer.js(210-ol3ex.js)」は、地図を表示するのに必要な javascript です。

「countries.geojson」データの内容は次のようになっていました。
「countries.geojson」
{"type":"FeatureCollection",
 "features":[{
  "type":"Feature",
  "id":"AFG",
  "properties":{"name":"Afghanistan"},
  "geometry":{
   "type":"Polygon",
   "coordinates":[[[61.210817,35.650072],...

「210-ol3ex.js」
var styleCache = {};
var vectorLayer = new ol.layer.Vector({
 source: new ol.source.GeoJSON({
  projection: 'EPSG:3857',
//url: 'data/geojson/countries.geojson',
  url: 'v3.0.0-beta.5/examples/data/geojson/countries.geojson',
 }),
 style: function(feature, resolution) {
  var text = resolution < 5000 ? feature.get('name') : '';
  // 解像度が 5000 以下なら ラベル(name 国名)非表示
  if (!styleCache[text]) {
  // 'styleCache[text]={}' なので 'if(true)'
   styleCache[text] = [new ol.style.Style({
    fill: new ol.style.Fill({
     color: 'rgba(255, 255, 255, 0.6)'
    }),
    stroke: new ol.style.Stroke({
     color: '#319FD3',
     width: 1
    }),
    text: new ol.style.Text({
     font: '12px Calibri,sans-serif',
     text: text,
     fill: new ol.style.Fill({
      color: '#000'
     }),
     stroke: new ol.style.Stroke({
      color: '#fff',
      width: 3
     })
    })
   })];
  }
  return styleCache[text];
 }
});
var map = new ol.Map({
 layers: [
  new ol.layer.Tile({
   source: new ol.source.MapQuest({layer: 'sat'})
  }),
  vectorLayer
 ],
 target: 'map',
 view: new ol.View2D({
  center: [0, 0],
  zoom: 1
 })
});
var highlightStyleCache = {};
var featureOverlay = new ol.FeatureOverlay({
// 一時的にフィーチャのスタイルを換えるときに使用(Canvas renderer)
 map: map,
 style: function(feature, resolution) {
  var text = resolution < 5000 ? feature.get('name') : '';
  if (!highlightStyleCache[text]) {
   highlightStyleCache[text] = [new ol.style.Style({
    stroke: new ol.style.Stroke({
     color: '#f00',
     width: 1
    }),
    fill: new ol.style.Fill({
     color: 'rgba(255,0,0,0.1)'
    }),
    text: new ol.style.Text({
     font: '12px Calibri,sans-serif',
     text: text,
     fill: new ol.style.Fill({
      color: '#000'
     }),
     stroke: new ol.style.Stroke({
      color: '#f00',
      width: 3
     })
    })
   })];
  }
  return highlightStyleCache[text];
 }
});
var displayFeatureInfo = function(pixel) {
 var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
 // 'forEachFeatureAtPixel' viewport 上を横切るピクセルフィーチャを検知
  return feature;
 });
 // 国名(name)の表示 
 var info = document.getElementById('info');
 if (feature) {
  info.innerHTML = feature.getId() + ': ' + feature.get('name');
 } else {
  info.innerHTML = ' ';
 }
 // フィーチャのハイライトのオンオフ
 if (feature !== highlight) {
  if (highlight) {
   featureOverlay.removeFeature(highlight);
  }
  if (feature) {
   featureOverlay.addFeature(feature);
  }
   highlight = feature;
 }
};
/*
 * マウス(ポインタ)が動いた時、ビューポートのピクセル位置を取得して
 * displayFeatureInfo を実行。
 */
$(map.getViewport()).on('mousemove', function(evt) {
 var pixel = map.getEventPixel(evt.originalEvent);
 displayFeatureInfo(pixel);
});
// マウスクリック時?
map.on('click', function(evt) {
 displayFeatureInfo(evt.pixel);
});

var highlight;

2 - ol3-beta.5ex 10a - Vector layer example 1

「Vector layer example(vector-layer.html)」を参考に地図を表示してみます。
HTML ファイルの作成
a Eclipse のメニューの「ファイル」->「ファイルを開く」をクリックします。





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





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



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








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











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


「210-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.0.0-beta.5/css/ol.css" type="text/css">
  <link rel="stylesheet" href="v3.0.0-beta.5/resources/bootstrap/css/bootstrap.min.css" type="text/css">
  <link rel="stylesheet" href="v3.0.0-beta.5/resources/layout.css" type="text/css">
  <link rel="stylesheet" href="v3.0.0-beta.5/resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
  <title>Vector layer example</title>
 </head>
 <body>

  <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.0.0-beta.5/examples/"><img src="v3.0.0-beta.5/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="span4">
     <h4 id="title">Vector layer example</h4>
     <p id="shortdesc">Example of a countries vector layer with country information on hover and country labels at higher zoom levels.</p>
     <div id="docs">
<!--
      <p>See the <a href="vector-layer.js" target="_blank">vector-layer.js source</a> to see how this is done.</p>
-->
       <!-- ファイル修正 -->
      <p>See the <a href="210-ol3ex.js" target="_blank">210-ol3ex.js source</a> to see on how this is done.</p>
     </div>
     <div id="tags">vector, geojson, style, feature overlay</div>
    </div>
    <div class="span4 offset4">
     <div id="info" class="alert alert-success">
       
     </div>
    </div>
   </div>
  </div>
<!--
  <script src="jquery.min.js" type="text/javascript"></script>
  <script src="../resources/example-behaviour.js" type="text/javascript"></script>
-->
  <!-- ディレクトリ修正 -->
  <script src="v3.0.0-beta.5/examples/jquery.min.js" type="text/javascript"></script>
  <script src="v3.0.0-beta.5/resources/example-behaviour.js" type="text/javascript"></script>
<!--
  <script src="loader.js?id=vector-layer" type="text/javascript"></script>
-->
  <!-- ファイル修正 -->  <!-- ディレクトリ修正 -->
  <script src="loader.js?id=210-ol3ex" type="text/javascript"></script>
 </body>
</html>