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

2014年10月1日水曜日

2 - ol3ex 13b - Mouse position example 2

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

「213-ol3ex.js」
var mousePositionControl = new ol.control.MousePosition({
/** ol.control.MousePosition 
 * A control to show the 2D coordinates of the mouse 
 * cursor. By default, these are in the view projection, 
 * but can be in any supported projection. By default 
 * the control is shown in the top right corner of the 
 * map, but this can be changed by using the css selector 
 * .ol-mouse-position.
 * マウスカーソルの2次元座標を表示すするためのコントロール。デ
 * フォルトでは、これらはビュー投影(の数値)ですが、サポートさ
 * れている任意の投影にすることができます。デフォルトでは、コン
 * トロールは、マップの右上に表示されますが、これは CSS セレク
 * タ .ol-mouse-position を使用して変更することができます。
 * (ol3 API)
 */
  coordinateFormat: ol.coordinate.createStringXY(4),
 /** ol.coordinate.createStringXY(opt_fractionDigits)
  * Returns a ol.CoordinateFormatType function that can be 
  * used to format a {ol.Coordinate} to a string.
  * 「ol.Coordinate」を文字列にフォーマットするために使用される 
  * ol.CoordinateFormatType 関数を返します。(小数点以下の桁数)
  * (ol3 API)
  */
  projection: 'EPSG:4326',
  // comment the following two lines to have the mouse 
  // position be placed within the map.
  className: 'custom-mouse-position', // CSS クラス名
  target: document.getElementById('mouse-position'),
  undefinedHTML: ' '
});
// 追加
var projection = ol.proj.get('EPSG:2451');
/** ol.proj.get(projectionLike)
 * Fetches a Projection object for the code specified.
 * 指定されたコードのプロジェクション·オブジェクトを取得
 * (ol3 API)
 */
projection.setExtent([-85044.6807, -406697.3730, 96656.4509, -8388.7498]);
/** setExtent(extent)
 * Set the validity extent for this projection.
 * この投影の有効範囲を設定します。(ol3 API)
 */
var extent = [-63100, -34500, -45400, -24200];
var map = new ol.Map({
 controls: ol.control.defaults({
 /** controls
  * Controls initially added to the map. 
  * If not specified, ol.control.defaults() is used.
  * 初期設定で、マップに追加されたコントロール。
  * 明示されていなければ、ol.control.defaults() が使用されます。
  * (ol3 API)
  */
 /** ol.control.defaults()
  * デフォルトでは、マップに含まコントロールのセット。
  * 特に設定しない限り、これは、以下の各コントロールの
  * インスタンスを含むコレクションを返します。(ol3 API)
  * ol.control.Zoom, ol.control.Rotate, ol.control.Attribution
  */
  attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
  /** @type 
   * 値のタイプ(型)の説明 - 式などで表示
   * (@use JSDoc[http://usejsdoc.org/]より)
   */
   collapsible: false // 折りたたみ
  })
 }).extend([
  mousePositionControl
 ]),
 layers: [
/*
 * new ol.layer.Tile({
 *  source: new ol.source.OSM()
 * }),
 */
  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.TileWMS(({
   /** ol.source.TileWMS
    * Layer source for tile data from WMS servers.
    * WMS サーバからのタイルデータのためのソース。(ol3 API)
    */
    url: 'http://localhost:8080/geoserver/wms?',
    attributions: [new ol.Attribution({
   /** ol.Attribution
    * An attribution for a layer source.
    * レイヤソースの属性(ol3 API)
    */
     html: '国土交通省 国土数値情報'
     // html: '<a href="http://portal.cyberjapan.jp/help/termsofuse.html" target="_blank">' + // EPSG:2451
     // '国土地理院</a>' // EPSG:2451
    })],
    params: {'LAYERS': 'npn:tokyo_kuiki', 'TILED': true},
    // params: {'LAYERS': 'npn:akiruno_kukaku', 'TILED': true, 'VERSION': '1.1.1'}, // EPSG:2451
    // extent: extent, // EPSG:2451
    serverType: 'geoserver'
   }))
  })
 ],
  renderer: exampleNS.getRendererFromQueryString(),
  //'example-behavior.js' により URL にある renderer を返します
  target: 'map',
  view: new ol.View({
   projection: 'EPSG:4326', // 追加
   // projection: projection, // EPSG:2451
   // extent: extent, // EPSG:2451
// center: [0, 0],
   center: [139.42, 35.68],
   // center: [-54250, -29350], // EPSG:2451
// zoom: 2
   zoom: 10
   // zoom: 6 // EPSG:2451
  })
});
var projectionSelect = new ol.dom.Input(document.getElementById('projection'));
/** ol.dom.Input
 * Helper class for binding HTML input to an ol.Object.
 * ol.Object に HTML input要素を接続するためのヘルパークラス。
 * (ol3 API)
 */
projectionSelect.bindTo('value', mousePositionControl, 'projection')
 .transform(
/** bindTo()
 * The bindTo method allows you to set up a two-way 
 * binding between a `source` and `target` object. 
 * The method returns an object with a `transform` 
 * method that you can use to provide `from` and 
 * `to` functions to transform values on the way 
 * from the source to the target and on the way back.
 * bindTo メソッドは、`source` と ` target` オブジェク
 * ト間の結合を双方向に設定することができます。メソッド
 * は、ソースからターゲットに、および、その逆方向に値を
 * 変換する、 `from` と ` to` ファンクションを提供する
 * ために使用することがでる `transform` メソッドをとも
 * なった、オブジェクトを返します。
 * (ol3 API[説明は Stable Only のチェックを外すと表示])
 */
/** transform(source, destination)
 * Transform each coordinate of the geometry from one 
 * coordinate reference system to another. The 
 * geometry is modified in place. For example, a line 
 * will be transformed to a line and a circle to a 
 * circle. If you do not want the geometry modified in 
 * place, first clone() it and then use this function 
 * on the clone.
 * ある座標参照系から別のものへジオメトリの各座標を変換。ジオ
 * メトリは、所定の位置に修正されます。例えば、ラインはライン
 * に、円は円に変換されます。ジオメトリを所定の位置に修正した
 * くない場合、最初にそれを clone() し、それからクローンでこ
 * の関数を使用します。(ol3 API)
 */
  function(code) {
  //projectionSelect.value -> mousePositionControl.projection
   return ol.proj.get(/** @type {string} */ (code));
  },
  function(projection) {
  //mousePositionControl.projection -> projectionSelect.value
   return projection.getCode();
  /** getCode()
   * Get the code for this projection, e.g. 'EPSG:4326'.
   * この東映(例えば EPSG:4326)の座標を取得。(ol3 API)
   */
});
var precisionInput = document.getElementById('precision');
precisionInput.addEventListener('change', function() {
/** EventTarget.addEventListener
 * addEventListener は、 1 つのイベントターゲットにイベント 
 * リスナーを1つ登録します。イベントターゲットは、ドキュメント
 * 上の単一のノード、ドキュメント自身、ウィンドウ、あるいは、
 * XMLHttpRequest です。
 *(MDN[https://developer.mozilla.org/ja/docs/Web/API/
 * EventTarget.addEventListener])
 */
  var format = ol.coordinate.createStringXY(precisionInput.valueAsNumber);
 /** valueAsNumber
  * The value of the element, interpreted as one of 
  * the following in order: 1.a time value, 
  * 2.a number, 3.NaN if conversion is not possible
  * 要素の値。次の順に解釈されます。
  * (MDN[https://developer.mozilla.org/en-US/docs/Web/
  * API/HTMLInputElement])
  */
  mousePositionControl.setCoordinateFormat(format);
 /** setCoordinateFormat(format)
  * Name: format, Type: ol.CoordinateFormatType, 
  * Description: The format to render the current 
  * position in.
  * (ol3 API)
  */
}, false);



2 - ol3ex 13a - Mouse position example 1

「Mouse position example(mouse-position.html)」を参考に地図を表示してみます。「EPSG:4326 example(epsg-4326.html)」と ブログ「2 - ol3ex 3a - Single image WMS with Proj4js projection example 1 (http://nobmob.blogspot.jp/2014/09/2-ol3ex-3a-single-image-wms-with.html)」を参考に、座標系が EPSG:4326 と EPSG:2451 について試しました。
HTML ファイルの作成
a Eclipse のメニューの「ファイル」->「ファイルを開く」をクリックします。





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





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



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








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











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


「213-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>Mouse position 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="span6">
     <h4 id="title">Mouse position example</h4>
     <p id="shortdesc">Example of a mouse position control, outside the map.</p>
      <div id="docs">
<!--
       <p>See the <a href="mouse-position.js" target="_blank">mouse-position.js source</a> to see how this is done.</p>
-->
        <!-- ファイル修正 -->
       <p>See the <a href="213-ol3ex.js" target="_blank">213-ol3ex.js source</a> to see how this is done.</p>
      </div>
      <div id="tags">mouse-position, openstreetmap</div>
      <form>
       <label>Projection </label>
       <select id="projection">
        <option value="EPSG:4326">EPSG:4326</option>
        <option value="EPSG:3857">EPSG:3857</option>
        <option value="EPSG:2451">EPSG:2451</option><!-- 追加 -->
       </select>
       <label>Precision </label>
       <input id="precision" type="number" min="0" max="12" value="4"/>
      </form>
     </div>
     <div class="span6" id="mouse-position"> </div>
    </div>
   </div>
<!-- 追加 -->
  <script src="proj4js-2.2.2/dist/proj4.js" type="text/javascript"></script>
  <script src="proj4js-2.2.2/dist/EPSG2451.js" type="text/javascript"></script>
<!--
  <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=mouse-position" type="text/javascript"></script>
-->
  <!-- ファイル修正 -->  <!-- ディレクトリ修正 -->
  <script src="loader.js?id=213-ol3ex" type="text/javascript"></script>

 </body>
</html>

2014年7月24日木曜日

2 - ol3-beta.5ex 14b - Mouse position example 2

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

あきる野市(EPSG:2451)の地図を表示したとき、EPSG:3857 のマウスポジションの数値が正しく表示されませんでした。(EPSG:2451 の数値が表示)

「214-ol3ex.js」
var mousePositionControl = new ol.control.MousePosition({
  coordinateFormat: ol.coordinate.createStringXY(4), // 小数点以下
  projection: 'EPSG:4326',
  // comment the following two lines to have the mouse position
  // be placed within the map.
  className: 'custom-mouse-position', // CSS クラス名
  target: document.getElementById('mouse-position'),
  undefinedHTML: ' '
});
// 追加
var projection = ol.proj.configureProj4jsProjection({
 code: 'EPSG:2451',
 extent: [-85044.6807, -406697.3730, 96656.4509, -8388.7498]
});
var extent = [-63100, -34500, -45400, -24200];
var map = new ol.Map({
 controls: ol.control.defaults().extend([mousePositionControl]),
 layers: [
/*
 * new ol.layer.Tile({
 *  source: new ol.source.OSM()
 * }),
 */
  new ol.layer.Tile({
   source: new ol.source.TileWMS(({
    url: 'http://localhost:8080/geoserver/wms?',
    attributions: [new ol.Attribution({
//     html: '国土数値情報'
     html: '<a href="http://portal.cyberjapan.jp/help/termsofuse.html" target="_blank">' + // EPSG:2451<
      '国土地理院</a>' // EPSG:2451
    })],
//    params: {'LAYERS': 'npn:tokyo_kuiki', 'TILED': true},
    params: {'LAYERS': 'npn:akiruno_kukaku', 'TILED': true, 'VERSION': '1.1.1'}, // EPSG:2451
    extent: extent, // EPSG:2451
    serverType: 'geoserver'
   }))
  })
 ],
  // 'example-behavior.js' により URL にある renderer を返します
  renderer: exampleNS.getRendererFromQueryString(),
  target: 'map',
  view: new ol.View2D({
//   projection: 'EPSG:4326', // 追加
   projection: projection, // EPSG:2451
   extent: extent, // EPSG:2451
// center: [0, 0],
//   center: [139.42, 35.68],
   center: [-54250, -29350], // EPSG:2451
// zoom: 2
//   zoom: 10
   zoom: 6 // EPSG:2451
  })
});
var projectionSelect = new ol.dom.Input(document.getElementById('projection'));
// 'Input(ol3)' ol.Object を HTML input要素に接続するためのヘルパークラス
projectionSelect.bindTo('value', mousePositionControl, 'projection')
 .transform(
// 'bindTo(ol3)' source と target の間を接続するメソッド。
  function(code) {
   // projectionSelect.value -> mousePositionControl.projection
   return ol.proj.get(/** @type {string} */ (code));
  },
  function(projection) {
   // mousePositionControl.projection -> projectionSelect.value
   return projection.getCode();
});
var precisionInput = document.getElementById('precision');
precisionInput.addEventListener('change', function() {
  var format = ol.coordinate.createStringXY(precisionInput.valueAsNumber);
  mousePositionControl.setCoordinateFormat(format);
}, false);



2 - ol3-beta.5ex 14a - Mouse position example 1

「Mouse position example(mouse-position.html)」を参考に地図を表示してみます。「EPSG:4326 example(epsg-4326.html)」と ブログ「2 - ol3ex 2a - Tiled WMS with Proj4js projection example(http://nobmob.blogspot.jp/2014/06/2-ol3ex-2a-tiled-wms-with-proj4js.html)」を参考に、座標系が EPSG:4326 と EPSG:2451 について試しました。

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





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





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



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








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











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

「214-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>Mouse position 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="span6">
     <h4 id="title">Mouse position example</h4>
     <p id="shortdesc">Example of a mouse position control, outside the map.</p>
      <div id="docs">
<!--
       <p>See the <a href="mouse-position.js" target="_blank">mouse-position.js source</a> to see how this is done.</p>
-->
        <!-- ファイル修正 -->
       <p>See the <a href="214-ol3ex.js" target="_blank">214-ol3ex.js source</a> to see how this is done.</p>
      </div>
      <div id="tags">mouse-position, openstreetmap</div>
      <form>
       <label>Projection </label>
       <select id="projection">
        <option value="EPSG:4326">EPSG:4326</option>
        <option value="EPSG:3857">EPSG:3857</option>
        <option value="EPSG:2451">EPSG:2451</option><!-- 追加 -->
       </select>
       <label>Precision </label>
       <input id="precision" type="number" min="0" max="12" value="4"/>
      </form>
     </div>
     <div class="span6" id="mouse-position"> </div>
    </div>
   </div>
<!-- 追加 -->
  <script src="proj4js/lib/proj4js-compressed.js" type="text/javascript"></script>
  <script src="proj4js/lib/defs/EPSG2451.js" type="text/javascript"></script>
<!--
  <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=mouse-position" type="text/javascript"></script>
-->
  <!-- ファイル修正 -->  <!-- ディレクトリ修正 -->
  <script src="loader.js?id=214-ol3ex" type="text/javascript"></script>

 </body>
</html>

2008年10月16日木曜日

OpenLayers 15h マウス位置の座標表示

MousePosition Control(mouse-position.html) を参考に、マウスの位置の座標を表示してみます。
地図の表示スクリプト部分に OpenLayers.Control.MousePosition を次のように追加します。

---
map.addLayers([layer1, layer3, layer4, layer5, layer6, layer7, layer8]);
var panel = new OpenLayers.Control.NavToolbar({'div':OpenLayers.Util.getElement('paneldiv')});
map.addControl(panel);
map.zoomToMaxExtent();
map.setOptions({restrictedExtent: extent});
var click = new OpenLayers.Control.Click();
map.addControl(click);
click.activate();
map.events.register("moveend", null, displayZoom);
map.fractionalZoom = input.checked;
map.zoomTo(Math.round(map.zoom));
nav = new OpenLayers.Control.Navigation({'zoomWheelEnabled': false});
map.addControl(nav);
map.addControl(new OpenLayers.Control.LayerSwitcher());
var controlOptions = {
mapOptions: mapOptions
};
var overview = new OpenLayers.Control.OverviewMap(controlOptions);
map.addControl(overview);
overview.maximizeControl();

//ここから追加
var mouseposition = new OpenLayers.Control.MousePosition();
map.addControl(mouseposition);
map.events.register("mousemove", map, function(e) {
var position = this.events.getMousePosition(e);
OpenLayers.Util.getElement("coords").innerHTML = position;
});
//ここまで
}
function displayZoom() {
document.getElementById("zoom").innerHTML = map.zoom.toFixed(4);
}
</script>
---

<body> タグ内に次のように <div> タグを追加し、画像内の位置をピクセル単位で表示します。

---
<div id="map"></div>
<div id="paneldiv" class="olControlNavToolbar"></div>

<!-- ここを追加 X, Yの表示-->
<div id="coords" style="margin-left:50px;"></div>
<div style="margin-left:50px;">Moving your mouse to the upper left corner of this map should return 'x=0,y=0'</div>

<div style="margin-left:50px;">zoom: <span id="zoom"></span></div>
<div style="margin-left:50px;"><a href="#" onclick="nav.enableZoomWheel();return false">Turn on Wheel Zoom</a> | <a href="#" onclick="nav.disableZoomWheel(); return false;">Turn off Wheel Zoom</a></div>
---