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

2015年10月18日日曜日

OL3-Cesium 9 - ol3cesium selection example 2

9-2 JavaScript ファイルの作成
「selection.js(9-ol3cesium18.js)」は、マップを表示するための JavaScript ファイルです。

OL3-Cesium API は、現在、すべて実験的(experimental)なものです。

「9-ol3cesium18.js」
var raster = 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 参照)
  */
});
var vector = new ol.layer.Vector({
/** ol.layer.Vector
 * Vector data that is rendered client-side.
 * クライアント側で描画されたベクタデータ。(ol3 API)
 */
 source: new ol.source.Vector({
 /** ol.source.Vector 
  * Provides a source of features for vector layers.
  * ベクタレイヤのフィーチャのソースを提供します。(ol3 API)
  */
  format: new ol.format.GeoJSON(),
  /** ol.format.GeoJSON 
   * Feature format for reading and writing data 
   * in the GeoJSON format.
   * GeoJSON フォーマットのデータを読み書きするための
   * フィーチャフォーマット。(ol3 API)
   */
  // url: 'data/geojson/countries.geojson'
  url: './js/libs/ol3-cesium-v1.8/examples/data/geojson/countries.geojson'
 })
});
var map = new ol.Map({
 layers: [raster, vector],
 target: 'map2d',
 view: new ol.View({
  center: [0, 0],
  zoom: 2
 })
});
var ol3d = new olcs.OLCesium({map: map, target: 'map3d'});
/** new olcs.OLCesium(options)
 * map: The OpenLayers map we want to show on a Cesium scene.
 * Cesium シーンで表示したい OpenLayers マップ。
 * (OL3-Cesium API)
 */
var scene = ol3d.getCesiumScene();
/** getCesiumScene()
 * (OL3-Cesium API に説明がありませんでした。)
 */
ol3d.setEnabled(true);
/** setEnabled(enable)
 * Enables/disables the Cesium. This modifies the visibility 
 * style of the container element.
 * セシウムを有効または無効にします。これは、コンテナ要素の可視
 * 性スタイルを変更します。
 * (OL3-Cesium API)
 */
var selectionStyle = new ol.style.Style({
/** ol.style.Style 
 * Base class for vector feature rendering styles.
 * ベクタフィーチャがスタイルを描画するための基本クラス。
 * (ol3 API[説明は Stable Only のチェックを外すと表示])
 */
 fill: new ol.style.Fill({
 /** ol.style.Fill 
  * Set fill style for vector features.
  * ベクタフィーチャの塗りつぶしスタイルを設定。(ol3 API)
  */
  color: [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[説明は Stable Only のチェックを外すと表示])
  */
  color: [0, 153, 255, 1],
  width: 3
 })
});
var selectedFeature;
map.on('click', function(e) {
/** on(type, listener, opt_this)
 * Listen for a certain type of event.
 * あるタイプのイベントをリッスンします。(ol3 API)
 */
 if (selectedFeature) {
  selectedFeature.setStyle(null);
  /** setStyle(style)
   * Set the style for the feature. This can be a single 
   * style object, an array of styles, or a function that 
   * takes a resolution and returns an array of styles. If 
   * it is null the feature has no style (a null style).
   * フィーチャのスタイルを設定します。これは、単一のスタイルオブ
   * ジェクト、スタイルの配列、または解像度をとり、スタイルの配列
   * を返す関数とすることができます。null の場合、フィーチャは、
   * スタイルなし(null のスタイル)を持ちます。(ol3 API)
   */
 }
 selectedFeature = map.forEachFeatureAtPixel(
 /** forEachFeatureAtPixel(pixel, callback, opt_this, 
  * opt_layerFilter, opt_this2)
  * 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. 
  * ビューポート上のピクセルと交差するフィーチャを検出し、互
  * いに交差するフィーチャと共にコールバックを実行します。
  * 検出に含まれるレイヤが opt_layerFilter を通じて設定する
  * ことができます。(ol3 API)
  */
  e.pixel,
  function(feature, layer) {
   return feature;
 });
 if (selectedFeature) {
  selectedFeature.setStyle(selectionStyle);
 }
});
Chromium では表示できないので、Iceweasel(Firefox)のアドレスバーに

http://localhost/~user/ol3cesiumproj/public_html/9-ol3cesium18.html

と入力して表示します。
92a-ol3cesium17.png

OL3-Cesium 9 - ol3cesium selection example 1

OL3-Cesium Examples
http://openlayers.org/ol3-cesium/examples/

の例をみていきます。

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

A country may be highlighted by clicking it on the ol3 map.
It gets automatically selected in Cesium.
If you see flickering in Cesium, please check your graphic card drivers.
国が、ol3 マップ上でクリックすることによって強調表示されます。
これにより、セシウムでも自動的に選択します。
セシウムでちらつきが見られた場合は、使用しているグラフィックカードドライバを確認してください。

9-1 HTML ファイルの作成
1 NetBeans を起動します。









2 「プロジェクト」ペインでツリーを ol3cesiumproj -> サイト・ルート -> js -> libs -> ol3-cesium-v1.8 -> examples とクリックして展開し selection.html をダブルクリックして開きます。selection.js もダブルクリックして開きます。



3 「新規ファイル」ボタンをクリックします。

4 ステップ「1.ファイル・タイプを選択」の「カテゴリ」で「HTML5」、「ファイルタイプ」で「HTMLファイル」を選択して「次>」ボタンをクリックします。






5 「new HTML ファイル」ダイアログで「ファイル名」を「9-ol3cesium18」と入力して「終了」ボタンをクリックします。







保存フォルダを変更するときは、「フォルダ」右の「参照」ボタンをクリックして「フォルダを参照」で「ol3cesiumproj-サイト・ルート」をクリックして選択し「フォルダを選択」ボタンをクリックします。









6 「selection.html」の内容をコピーして「9-ol3cesium18.html」に貼り付け、修正します。

7 同じように、「新規ファイル」ボタンをクリックし、ステップ「1.ファイル・タイプを選択」の「カテゴリ」で「HTML5」、「ファイルタイプ」で「JavaScriptファイル」をクリックして選択し「次>」ボタンをクリック。「ファイル名」を「9-ol3cesium18」と入力して「終了」ボタンをクリック。「selection.js」の内容をコピーして貼り付け、修正します。



「index.html」
<html>
 <head>
  <title>TODO supply a title</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
 </head>
 <body>
  <div>TODO write content</div>
  <div><a href="./3-ol3cesium18.html">3-ol3cesium18.html</a></div>
  <div><a href="./4-ol3cesium18.html">4-ol3cesium18.html</a></div>
  <div><a href="./5-ol3cesium18.html">5-ol3cesium18.html</a></div>
  <div><a href="./6-ol3cesium18.html">6-ol3cesium18.html</a></div>
  <div><a href="./7-ol3cesium18.html">7-ol3cesium18.html</a></div>
  <div><a href="./8-ol3cesium18.html">8-ol3cesium18.html</a></div>
  <div><a href="./9-ol3cesium18.html">9-ol3cesium18.html</a></div>
 </body>
</html>


「9-ol3cesium18.html」
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="robots" content="index, all" />
  <title>ol3cesium selection example</title>
  <!-- ディレクトリ修正
  <link rel="stylesheet" href="../ol3/css/ol.css" type="text/css">
  -->
  <link rel="stylesheet" href="./js/libs/ol3-cesium-v1.8/ol3/css/ol.css" type="text/css">
 </head>
 <body>
  <div id="map2d" style="width:600px;height:400px;float:left;"></div>
  <div id="map3d" style="width:600px;height:400px;float:left;"></div>
  <div>A country may be highlighted by clicking it on the 
   ol3 map.
  <br/>It gets automatically selected in Cesium.
  <br/>If you see flickering in Cesium, please check your 
   graphic card drivers.</div>
  <!-- ディレクトリ修正
  <script src="../ol3/ol-debug.js"></script>
  <script src="../Cesium/Cesium.js"></script>
  <script src="../ol3cesium.js"></script>
  -->
  <script src="./js/libs/ol3-cesium-v1.8/ol3/ol-debug.js"></script>
  <script src="./js/libs/ol3-cesium-v1.8/Cesium/Cesium.js"></script>
  <script src="./js/libs/ol3-cesium-v1.8/ol3cesium.js"></script>
  <!-- <script src="selection.js"></script> -->
  <script src="9-ol3cesium18.js"></script>
 </body>
</html>

2015年2月3日火曜日

2 - ol3.1ex 51b - Box selection example 2

「box-selection.js(251-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],...

「251-ol3ex.js」
var vectorSource = 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.1.1/examples/data/geojson/countries.geojson'
});
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.OSM()
   /** ol.source.OSM 
    * Layer source for the OpenStreetMap tile server.
    * OpenStreetMap タイルサーバのレイヤソース。(ol3 API)
    */
  }),
  new ol.layer.Vector({
  /** ol.layer.Vector
   * Vector data that is rendered client-side.
   * クライアント側で描画されたベクタデータ。(ol3 API)
   */
   source: vectorSource
  })
 ],
 renderer: 'canvas',
 target: 'map',
 view: new ol.View({
  center: [0, 0],
  zoom: 2
 })
});
// a normal select interaction to handle click
// クリックを処理するための通常のセレクトインターラクション
var select = new ol.interaction.Select();
/** ol.interaction.Select 
 * Handles selection of vector data. A 
 * ol.FeatureOverlay is maintained internally to 
 * store the selected feature(s). Which features 
 * are selected is determined by the condition 
 * option, and optionally the toggle or add/remove 
 * options.
 * ベクタデータの選択を処理します。 ol.FeatureOverlay 
 * は、選択したフィーチャを格納するために内部的に維持され
 * ています。選択されているどのフィーチャでも条件オプショ
 * ン、そして部分的にトグルまたは追加/削除オプションによっ
 * て決定されます。(ol3 API)
 */
map.addInteraction(select); 
/** addInteraction(interaction)
 * Add the given interaction to the map.
 * マップへ与えられたインターラクションを追加します。(ol3 API)
 */
var selectedFeatures = select.getFeatures();
/** getFeatures()
 * Get the selected features.
 * 選択されたフィーチャを取得します。(ol3 API)
 */
/** a DragBox interaction used to select features by 
 * drawing boxes
 * ボックスを描画することによってフィーチャをセレクトするための 
 * DragBox インターラクション
 */
var dragBox = new ol.interaction.DragBox({
/** ol.interaction.DragBox 
 * Allows the user to draw a vector box by clicking and 
 * dragging on the map, normally combined with an 
 * ol.events.condition that limits it to when the shift 
 * or other key is held down. This is used, for example, 
 * for zooming to a specific area of the map (see
 *  ol.interaction.DragZoom and 
 *  ol.interaction.DragRotateAndZoom).
 * This interaction is only supported for mouse devices.
 * クリックとドラッグすることにより、ユーザーがベクタ
 * ボックスを描画することができます。通常は、シフトまた
 * はその他のキーが押されたときにそれを制限する
 *  ol.events.condition と組み合わせます。これは、例え
 * ば、マップの特定の領域へズーミングに使用されます。
 *(ol.interaction.DragZoom と 
 *(ol.interaction.DragRotateAndZoomを参照)
 * このインターラクションは、マウスデバイスでサポートさ
 * れています。(ol3 API)
 */
 condition: ol.events.condition.shiftKeyOnly,
 /** ol.events.condition.shiftKeyOnly
  * Returns: True if only the shift key is pressed.
  * (ol3 API)
  */
 style: new ol.style.Style({
 /** ol.style.Style 
  * Base class for vector feature rendering styles.
  * ベクタフィーチャがスタイルを描画するための基本クラス。
  * (ol3 API)
  */
  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: [0, 0, 255, 1]
  })
 })
});
map.addInteraction(dragBox);
var infoBox = document.getElementById('info');
dragBox.on('boxend', function(e) {
/** on()
 * Listen for a certain type of event.
 * ある型のイベントをリッスンします。
 * Returns: Unique key for the listener.(0l3 API)
 */
/**
 * features that intersect the box are added to the 
 * collection of selected features, and their names are 
 * displayed in the "info" div
 * ボックスを交差するフィーチャは、選択したフィーチャのコレクショ
 * ンに追加され、その名前が "info" の div に表示されます。
 */
 var info = [];
 var extent = dragBox.getGeometry().getExtent();
 /** getGeometry()
  * Returns geometry of last drawn box.
  * 最後に描画したボックスのジオメトリを返します。(ol3 API)
  */
 /** getExtent()
  * Get the extent of the geometry.
  * ジオメトリの範囲を取得します。(ol3 API)
  */
 vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) {
 /** forEachFeatureIntersectingExtent()
  * Iterate through all features whose geometry intersects 
  * the provided extent, calling the callback with each 
  * feature. If the callback returns a "truthy" value, 
  * iteration will stop and the function will return the 
  * same value. 
  * If you only want to test for bounding box intersection, 
  * call the source.forEachFeatureInExtent() method instead.
  * 提供範囲を交差するジオメトリのすべてのフィーチャを反復し、各
  * フィーチャと共にコールバックを呼び出します。コールバックが
  * 「truthy」の値を返した場合、反復が停止し、フィーチャは同じ値
  * を返します。
  * ボックス交差境界をテストするだけなら、代わりに 
  * source.forEachFeatureInExtent()メソッドを呼び出します。
  * (ol3 API)
  */
  selectedFeatures.push(feature);
  /** push(elem)
   * Insert the provided element at the end of the 
   * collection.
   * コレクションの最後に供給されたエレメントに挿入します。
   * Name: elem, Type: T, Description: Element
   * (ol3 API)
   */
  info.push(feature.get('name'));
  /** Array.push
   * 与えられた要素を追加することによって配列を変異させ、
   * その配列の新しい長さを返します。
   * (MDN [https://developer.mozilla.org/ja/docs/Web/
   * JavaScript/Reference/Global_Objects/Array/push])
   */
  /** get(key)
   * Gets a value.
   * (ol3 API[説明は Stable Only のチェックを外すと表示])
   */
 });
 if (info.length > 0) {
  infoBox.innerHTML = info.join(', ');
  /** Array.join
   * 配列の全ての要素を繋いで文字列にします。
   * (MDN [https://developer.mozilla.org/ja/docs/Web/
   * JavaScript/Reference/Global_Objects/Array/join])
   */
 }
});
/** clear selection when drawing a new box and when clicking 
 * on the map
 * 新しいボックスを描画したときとマップ場をクリックしたときに選択
 * を解除します。
 */
dragBox.on('boxstart', function(e) {
 selectedFeatures.clear();
 /** clear()
  * Remove all elements from the collection.
  * コレクションからすべてのエレメントを削除します。(ol3 API)
  */
 infoBox.innerHTML = '&nbsp;';
});
map.on('click', function() {
/** on
 * Listen for a certain type of event.
 * あるタイプのイベントをリッスンします。(ol3 API)
 */
 selectedFeatures.clear();
 infoBox.innerHTML = ' ';
});

2014年10月2日木曜日

2 - ol3ex 15b - Select features example 2

「select-features.js(215-ol3ex.js)」は、地図を表示するのに必要な javascript です。
「215-ol3ex.js」
var raster = 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 参照)
  */
});
var vector = 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'
 })
});
var map = new ol.Map({
 layers: [raster, vector],
 target: 'map',
 view: new ol.View({
  center: [0, 0],
  zoom: 2
 })
});
var select = null;  // ref to currently selected interaction
// select interaction working on "singleclick"
var selectSingleClick = new ol.interaction.Select();
/** ol.interaction.Select 
 * Handles selection of vector data. A 
 * ol.FeatureOverlay is maintained internally to 
 * store the selected feature(s). Which features 
 * are selected is determined by the condition 
 * option, and optionally the toggle or add/remove 
 * options.
 * ベクタデータの選択を処理します。 ol.FeatureOverlay 
 * は、選択したフィーチャを格納するために内部的に維持され
 * ています。選択されているどのフィーチャでも条件オプショ
 * ン、そして部分的にトグルまたは追加/削除オプションによっ
 * て決定されます。(ol3 API)
 */
// select interaction working on "click"
var selectClick = new ol.interaction.Select({
 condition: ol.events.condition.click
 /** ol.events.condition.click(mapBrowserEvent)
  * Name: mapBrowserEvent, Type: ol.MapBrowserEvent,
  * Description: Map browser event.(ol3 API)
  */
});
// select interaction working on "mousemove"
var selectMouseMove = new ol.interaction.Select({
 condition: ol.events.condition.mouseMove
 /** ol.events.condition.mouseMove(mapBrowserEvent)
  * Name: mapBrowserEvent, Type: ol.MapBrowserEvent,
  * Description: Map browser event.
  * (ol3 API[説明は Stable Only のチェックを外すと表示])
  * (v3.3.0 以降 ol.events.condition.pointerMove 
  * に変更されています。)
  */
});
var selectElement = document.getElementById('type');
var changeInteraction = function() {
 if (select !== null) {
  map.removeInteraction(select);
 /** removeInteraction(()
  * Remove the given interaction from the map.
  * マップから与えられたインターラクションを削除します。
  * (ol3 API)
  */
  }
 var value = selectElement.value;
 if (value == 'singleclick') {
  select = selectSingleClick;
 } else if (value == 'click') {
  select = selectClick;
 } else if (value == 'mousemove') {
  select = selectMouseMove;
 } else {
  select = null;
 }
 if (select !== null) {
  map.addInteraction(select);
  /** addInteraction(()
   * add the given interaction to the map.
   * マップへ与えられたインターラクションを追加します。
   * (ol3 API)
   */
 }
};
/**
 * onchange callback on the select element.
 */
selectElement.onchange = changeInteraction;
/** GlobalEventHandlers.onchange()
 * The onchange property sets and returns the event handler 
 * for the change event.
 * onchange プロパティは、change イベントに対してイベントハ
 * ンドラを設定、および、返します。
 * (MDN[https://developer.mozilla.org/en-US/docs/Web/
 * API/GlobalEventHandlers/onchange])
 */
changeInteraction();
 

2 - ol3ex 15a - Select features example 1

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






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





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



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








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











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


「215-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>Select features 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="span12">
     <h4 id="title">Select features example</h4>
     <p id="shortdesc">Example of using the Select interaction. Choose between <code>Single-click</code>, <code>Click</code> and <code>Hover</code> as the event type for selection in the combobox below. When using <code>Single-click</code> or <code>Click</code> you can hold do <code>Shift</code> key to toggle the feature in the selection.</p>
     <p>Note: when <code>Single-click</code> is used double-clicks won't select features. This in contrast to <code>Click</code>, where a double-click will both select the feature and zoom the map (because of the <code>DoubleClickZoom</code> interaction). Note that <code>Single-click</code> is less responsive than <code>Click</code> because of the delay it uses to detect double-clicks.</p>
      <form class="form-inline">
       <label>Action type  </label>
       <select id="type">
        <option value="none" selected>None</option>
        <option value="singleclick">Single-click</option>
        <option value="click">Click</option>
        <option value="mousemove">Hover</option>
       </select>
      </form>
      <div id="docs">
<!--
       <p>See the <a href="select-features.js" target="_blank">select-features.js source</a> to see how this is done.</p>
-->
       <!-- ファイル修正 -->
      <p>See the <a href="215-ol3ex.js" target="_blank">215-ol3ex.js source</a> to see how this is done.</p>
      </div>
      <div id="tags">select, vector</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=select-features" type="text/javascript"></script>
-->
  <!-- ファイル修正 -->  <!-- ディレクトリ修正 -->
  <script src="loader.js?id=215-ol3ex" type="text/javascript"></script>

  </body>
</html>

2014年7月24日木曜日

2 - ol3-beta.5ex 16b - Select features example 2

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

「216-ol3ex.js」
var raster = new ol.layer.Tile({
 source: new ol.source.MapQuest({layer: 'sat'})
});
var vector = 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'
 })
});
var select = new ol.interaction.Select();
var map = new ol.Map({
 interactions: ol.interaction.defaults().extend([select]),
 layers: [raster, vector],
 target: 'map',
 view: new ol.View2D({
  center: [0, 0],
  zoom: 2
 })
});
2161d-ol3ex.png

2 - ol3-beta.5ex 16a - Select features example 1

「Select features example(select-features.html)」を参考に地図を表示してみます。

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





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





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



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








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











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


「216-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>Select features 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="span12">
     <h4 id="title">Select features example</h4>
     <p id="shortdesc">Example of using the Select interaction.
     Select features by clicking polygons.
     Hold the Shift-key to toggle the feature in the selection.</p>
      <div id="docs">
<!--
       <p>See the <a href="select-features.js" target="_blank">select-features.js source</a> to see how this is done.</p>
-->
       <!-- ファイル修正 -->
      <p>See the <a href="216-ol3ex.js" target="_blank">216-ol3ex.js source</a> to see how this is done.</p>
      </div>
      <div id="tags">select, vector</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=select-features" type="text/javascript"></script>
-->
  <!-- ファイル修正 -->  <!-- ディレクトリ修正 -->
  <script src="loader.js?id=216-ol3ex" type="text/javascript"></script>

  </body>
</html>

2014年1月21日火曜日

34 - 編集ツールバー(Editing Toolbar) 2 - 選択ボタンを追加

34-2 Editing Toolbar に選択ボタンを追加する「ol015-nippon_bmi_akiruno_pgis.html」 ファイルを続けて使います。
OpenLayers の Development Examples の「Editing Toolbar Example(http://openlayers.org/dev/examples/editingtoolbar.html)」と OpenLayers の

「Control」ページ
http://docs.openlayers.org/library/controls.html

の「Customizing an Existing Panel」を参考に Editing Toolbar に選択ボタンを追加してみます。

---
   map = new OpenLayers.Map('map', {
    projection: new OpenLayers.Projection("EPSG:2451"),
    displayProjection: new OpenLayers.Projection("EPSG:4326"),
    maxResolution: 'auto',
    units: 'meters',
    maxExtent: new OpenLayers.Bounds(-63100,-34500,-45400,-24200),
    controls: [
     new OpenLayers.Control.PanZoom(),
//   new OpenLayers.Control.EditingToolbar(vectors ), 削除
     new OpenLayers.Control.LayerSwitcher(),
     new OpenLayers.Control.MousePosition()
    ]
   });
   map.addLayers([layer0, layer3, layer1, layer2, layer4, vectors]);
// ここから追加
   var panel = new OpenLayers.Control.Panel({
    displayClass: 'customEditingToolbar',
    allowDepress: true
    // "OpenLayers.Control.TYPE_TOOL" が "true" のとき "on" のアイコンをクリックすると "off" になる
   });
// ボタンを1つずつ設定しないと配置がうまくできませんでした
   panelCrontrol = [
    new OpenLayers.Control.Navigation(
     {
      title: "Navigation"
       // ポインタをボタンに重ねたときには浮き出して表示
     }),
),
    new OpenLayers.Control.DrawFeature(
     vectors,
     OpenLayers.Handler.Point,
     {
      multi: true,
       // レイヤに渡す前に feature をマルチタイプジオメトリに型変換
      displayClass: 'olControlDrawFeaturePoint' // アイコンの設定
    }),
    new OpenLayers.Control.DrawFeature(
     vectors,
     OpenLayers.Handler.Path,
     {
      multi: true,
      displayClass: 'olControlDrawFeaturePath'
    }),
    new OpenLayers.Control.DrawFeature(
     vectors,
     OpenLayers.Handler.Polygon,
     {
      multi: true,
      displayClass: 'olControlDrawFeaturePolygon'
    }),
    new OpenLayers.Control.SelectFeature(
     vectors,
     {
      clickout: false, toggle: false,
      multiple: false, hover: false,
      toggleKey: "ctrlKey", // ctrl key removes from selection
      multipleKey: "shiftKey", // shift key adds to selection
      box: true,
      displayClass: 'olControlSelectFeature'
    })
   ]; 
   panel.addControls(panelControl);
   map.addControl(panel);
// ここまで

   map.zoomToMaxExtent();
  }
 </script>
</head>
---

「displayClass」は、style を設定してアイコンを指定します。内容は "OpenLayers-2.13.1/theme/default/style.css" をコピーしました。
選択ボタンは次のようなものを作成して、"OpenLayers-2.13.1/theme/default/img/ に保存しました。

select_feature_on.png
select_feature_off.png

<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <link rel="stylesheet" href="OpenLayers-2.13.1/theme/default/style.css" type="text/css">
  <link rel="stylesheet" href="OpenLayers-2.13.1/examples/style.css" type="text/css">
<!-- ここから追加 -->
  <style>
   .customEditingToolbar {
    float: right;
    right: 0px;
    height: 30px; 
   }
   .customEditingToolbar div {
    float: right;
    margin: 5px;
    width: 24px;
    height: 24px;
   }
   .olControlNavigationItemActive { 
    background-image: url("OpenLayers-2.13.1/theme/default/img/editing_tool_bar.png");
    background-repeat: no-repeat;
    background-position: -103px -23px; 
   }
   .olControlNavigationItemInactive { 
    background-image: url("OpenLayers-2.13.1/theme/default/img/editing_tool_bar.png");
    background-repeat: no-repeat;
    background-position: -103px -0px; 
   }
  .olControlDrawFeaturePointItemInactive {
    background-image: url("OpenLayers-2.13.1/theme/default/img/editing_tool_bar.png");
    background-repeat: no-repeat;
    background-position: -77px -1px;
   }
  .olControlDrawFeaturePointItemActive {
    background-image: url("OpenLayers-2.13.1/theme/default/img/editing_tool_bar.png");
    background-repeat: no-repeat;
    background-position: -77px -24px;
   }
  .olControlDrawFeaturePathItemInactive {
    background-image: url("OpenLayers-2.13.1/theme/default/img/editing_tool_bar.png");
    background-repeat: no-repeat;
    background-position: -51px -1px;
   }
  .olControlDrawFeaturePathItemActive {
    background-image: url("OpenLayers-2.13.1/theme/default/img/editing_tool_bar.png");
    background-repeat: no-repeat;
    background-position: -51px -24px;
   }
   .olControlDrawFeaturePolygonItemInactive { 
    background-image: url("OpenLayers-2.13.1/theme/default/img/editing_tool_bar.png");
    background-repeat: no-repeat;
    background-position: -26px 0px; 
   }
   .olControlDrawFeaturePolygonItemActive { 
    background-image: url("OpenLayers-2.13.1/theme/default/img/editing_tool_bar.png");
    background-repeat: no-repeat;
    background-position: -26px -23px;
   }
   .olControlSelectFeatureItemActive {
    background-image: url(OpenLayers-2.13.1/theme/default/img/select_feature_on.png);
    background-repeat: no-repeat;
    background-position: 0 1px;
   }
   .olControlSelectFeatureItemInactive {
    background-image: url(OpenLayers-2.13.1/theme/default/img/select_feature_off.png);
    background-repeat: no-repeat;
    background-position: 0 1px;
   }
  </style>
<!-- ここまで -->
---


2014年1月14日火曜日

33 - マウスでフィーチャを描画 2 - ポリゴン(多角形)を選択する

33-2 ポリゴン(多角形)を選択する
examples フォルダにある「OpenLayers Select Feature Example(select-feature.html)」を参考に マウスで選択してみます。
「ol014-nippon_bmi_akiruno_pgis.html」 ファイルを続けて使います。

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







b 「ファイルを開く」ウィンドウで、「OpenLayers-2.13.1」->「examples」->「selsct-feature.html」をクリックして選択し、「OK」ボタンをクリックします。






c 次のように「selsct-feature.html」の内容の一部をコピーして「ol014-nippon_bmi_akiruno_pgis.html」に貼り付け、修正します。

---
<script type="text/javascript">
 var map, layer0, layer1, layer2, layer3, layer4, drawControls;
 OpenLayers.Feature.Vector.style['default']['strokeWidth'] = '2';
---
//  var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer"); 削除
// ここから追加
  // allow testing of specific renderers via "?renderer=Canvas", etc
  var renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
  renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;
  var vectors = new OpenLayers.Layer.Vector("Vector Layer", {
   renderers: renderer
  });
  vectors.events.on({
   'featureselected': function(feature) {
    document.getElementById('counter').innerHTML = this.selectedFeatures.length;
   },
   'featureunselected': function(feature) {
    document.getElementById('counter').innerHTML = this.selectedFeatures.length;
   }
  });
// ここまで
  map.addLayers([layer0, layer3, layer1, layer2, layer4, vectors]); // "polygonLayer" 削除,  "vectors" 追加
  drawControls = {
   polygon: new OpenLayers.Control.DrawFeature(
    vectors, OpenLayers.Handler.Polygon), // "polygonLayer" を "vectors" に変更
// ここから追加
   select: new OpenLayers.Control.SelectFeature(
    vectors,
    {
     clickout: false, toggle: false,
     multiple: false, hover: false,
     toggleKey: "ctrlKey", // ctrl key removes from selection
     multipleKey: "shiftKey", // shift key adds to selection
     box: true
    }
   ),
   selecthover: new OpenLayers.Control.SelectFeature(
    vectors,
    {
     multiple: false, hover: true,
     toggleKey: "ctrlKey", // ctrl key removes from selection
     multipleKey: "shiftKey" // shift key adds to selection
    }
   )
// ここまで
  };
  for(var key in drawControls) {
   map.addControl(drawControls[key]);
  }
//  document.getElementById('noneToggle').checked = true; 削除
  map.addControl(new OpenLayers.Control.LayerSwitcher());
  map.addControl(new OpenLayers.Control.MousePosition());
  map.zoomToMaxExtent();
 } // End of "function init()"
 function toggleControl(element) {
  for(key in drawControls) {
   var control = drawControls[key];
   if(element.value == key && element.checked) {
    control.activate();
   } else {
    control.deactivate();
    }
  }
 }
/* 削除
 function allowPan(element) {
  var stop = !element.checked;
  for(var key in drawControls) {
   drawControls[key].handler.stopDown = stop;
   drawControls[key].handler.stopUp = stop;
  }
 }
*/
// ここから追加
 function update() {
  var clickout = document.getElementById("clickout").checked;
  if(clickout != drawControls.select.clickout) {
   drawControls.select.clickout = clickout;
 }
 var box = document.getElementById("box").checked;
  if(box != drawControls.select.box) {
   drawControls.select.box = box;
   if(drawControls.select.active) {
    drawControls.select.deactivate();
    drawControls.select.activate();
   }
  }
 }
// ここまで
</script>
</head>
<body onload="init()">
---
 <ul id="controlToggle">
  <li>
   <input type="radio" name="type" value="none" id="noneToggle"
     onclick="toggleControl(this);" checked="checked" />
   <label for="noneToggle">navigate</label>
  </li>
  <li>
   <input type="radio" name="type" value="polygon" id="polygonToggle" onclick="toggleControl(this);" />
   <label for="polygonToggle">draw polygon</label>
  </li>
<!-- ここから追加 -->
  <li>
   <input type="radio" name="type" value="selecthover" id="selecthoverToggle" onclick="toggleControl(this);" />
   <label for="selecthoverToggle">Select features on hover</label>
  </li>
  <li>
   <input type="radio" name="type" value="select" id="selectToggle" onclick="toggleControl(this);" />
   <label for="selectToggle">select feature (<span id="counter">0</span> features selected)</label>
   <ul>
    <li>
     <input id="box" type="checkbox" checked="checked" name="box" onchange="update()" />
     <label for="box">select features in a box</label>
    </li>
    <li>
     <input id="clickout" type="checkbox" name="clickout" onchange="update()" />
     <label for="clickout">click out to unselect features</label>
    </li>
   </ul>
  </li>
<!--  ここまで -->
 </ul>
 </body>
</html>

Select features on hover:
ポインタをフィーチャの上に重ねると色が変わる。

select feature (0 features selected):
クリックするとフィーチャの色が変わる。次のフィーチャをクリックするとそのフィーチャの色が変わり、色がかわっていたフィーチャは元の色に戻る。フィーチャ以外の部分をクリックしても変化はない。
 select features in a box:
 地図上をドラッグして範囲選択すると、その中のフィーチャの色が変わる。
 click out to unselect features:
 フィーチャ以外の部分をクリックすると元の色に戻る。

Use the shift key to select multiple features.
shift キーを押しながらクリックすると複数のフィーチャの色が同時に変わる。

Use the ctrl key to  toggle selection on features one at a time.
ctrl キーを押しながらひとつのフィーチャをクリックすると、「色が変わる」、「元の色に戻る」を繰り返す。
複数のフィーチャの色が変わっているときは、そのフィーチャだけが元の色に戻り、もう一度クリックするとそのフィーチャだけが色が変わり、他のフィーチャは元の色に戻る。



Note: the "clickout" option has no effect when "hover" is selected.
clickout オプションは、hover が選択されているときは効果がない。