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

2015年3月12日木曜日

2 - ol3.3ex 82b - Export map example 2

「export-map.js(282-ol3ex.js)」は、マップを表示するための JavaScript ファイルです。
「282-ol3ex.js」
var map = new ol.Map({
 layers: [
  new ol.layer.Tile({
   source: new ol.source.OSM()
   /** ol.source.OSM 
    * Layer source for the OpenStreetMap tile server.
    * OpenStreetMap タイルサーバのレイヤソース。(ol3 API)
    */
  }),
  new ol.layer.Vector({
   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.3.0/examples/data/geojson/countries.geojson'
   })
  })
 ],
 target: '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 // 折りたたみ
  })
 }),
 view: new ol.View({
  center: [0, 0],
  zoom: 2
 })
});
var exportPNGElement = document.getElementById('export-png');
if ('download' in exportPNGElement) {
 exportPNGElement.addEventListener('click', function(e) {
 /** EventTarget.addEventListener
  * addEventListener は、 1 つのイベントターゲットにイベント 
  * リスナーを1 つ登録します。イベントターゲットは、ドキュメント
  * 上の単一のノード、ドキュメント自身、ウィンドウ、あるいは、
  * XMLHttpRequest です。
  *(MDN[https://developer.mozilla.org/ja/docs/Web/API/
  * EventTarget.addEventListener])
  */
  map.once('postcompose', function(event) {
  /** once()
   * Listen once for a certain type of event.
   * あるタイプのイヴェントを1回リッスンします。
   * Return: Unigue key for the listener.(ol3 API)
   */
  /** postcompose イベント
   * レイヤを描画した後に発生するイベント。
   * (「Layer spy example」参照)
   */
   var canvas = event.context.canvas;
   exportPNGElement.href = canvas.toDataURL('image/png');
   /** HTMLCanvasElement.toDataURL()
    * The HTMLCanvasElement.toDataURL() method returns a 
    * data URIs containing a representation of the image 
    * in the format specified by the type parameter 
    * (defaults to PNG). The returned image is in a 
    * resolution of 96 dpi.
    * HTMLCanvasElement.toDataURL() メソッドは、型引数
    * (デフォルトは PNG)で指定したフォーマットの画像の表現を
    * 含むデータのURIを返します。返された画像は、96dpi の解像
    * 度です。
    * (MDN[https://developer.mozilla.org/en-US/docs/Web/
    * API/HTMLCanvasElement/toDataURL])
    */
  });
  map.renderSync();
  /** renderSync()
   * Requests an immediate render in a synchronous manner.
   * 同期即時描画を要求します。(ol3 API)
   */
 }, false);
 } else {
 var info = document.getElementById('no-download');
 /**
  * display error message
  */
 info.style.display = '';
}

2 - ol3.3ex 82a - Export map example 1

「Export map example (export-map.html)」を参考に地図を表示してみます。

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





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





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



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








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











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


「282-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.3.0/css/ol.css" type="text/css">
  <link rel="stylesheet" href="v3.3.0/resources/bootstrap/css/bootstrap.min.css" type="text/css">
  <link rel="stylesheet" href="v3.3.0/resources/layout.css" type="text/css">
  <link rel="stylesheet" href="v3.3.0/resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
  <title>Export map example</title>
 </head>
 <body>
  <!-- 
  bootstrap.min.css, bootstrap-responsive.min.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.3.0/examples/"><img src="v3.3.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 id="no-download" class="alert alert-error" style="display: none">
      This example requires a browser that supports the
      <a href="http://caniuse.com/#feat=download">link download</a> attribute.
     </div>
     <a id="export-png" class="btn" download="map.png">
      <i class="icon-download"></i>Export PNG</a>
    </div>
   </div>
   <div class="row-fluid">
    <div class="span12">
    <h4 id="title">Export map example</h4>
     <p id="shortdesc">Example of exporting a map as a PNG image.</p>
     <div id="docs">
      <!--
      <p>See the <a href="export-map.js" target="_blank">export-map.js source</a> 
       to see how this is done.</p>
      -->
      <!-- ファイル修正 -->
      <p>See the <a href="282-ol3ex.js" target="_blank">282-ol3ex.js source</a> 
       to see how this is done.</p>
     </div>
     <div id="tags">export, png, openstreetmap</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.3.0/resources/jquery.min.js" type="text/javascript"></script>
  <script src="v3.3.0/resources/example-behaviour.js" type="text/javascript"></script>
  <!--
  <script src="loader.js?id=export-map" type="text/javascript"></script>
  -->
  <!-- ファイル修正 -->  <!-- ディレクトリ修正 -->
  <script src="loader.js?id=282-ol3ex" type="text/javascript"></script>
 </body>
</html>