osm4_ol_icon
差分
このページの2つのバージョン間の差分を表示します。
| 次のリビジョン | 前のリビジョン | ||
| osm4_ol_icon [2018/05/22 10:40] – 作成 管理者 | osm4_ol_icon [2018/05/22 13:54] (現在) – 管理者 | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | [[start]]>[[programming_start]] | ||
| + | ====== OpenLayers4での自作アイコン表示(仮) ====== | ||
| + | 注意:「OL4勉強のための試作プログラムなので間違っている可能性があります」「DokuWikiは個別にヘッダを編集できないので通常とは少し違う構造になっています」 | ||
| + | |||
| + | 東京都庁と千葉県庁の自作アイコンを表示するサンプルです。 | ||
| + | |||
| + | ===== デモ ===== | ||
| + | < | ||
| + | < | ||
| + | .mapdiv{height: | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | /* 外部CSSの読み込み */ | ||
| + | function cssLoad(cssFile){ | ||
| + | if(document.all){ | ||
| + | document.createStyleSheet(cssFile); | ||
| + | }else{ | ||
| + | var link = document.createElement(" | ||
| + | link.rel = " | ||
| + | link.href = cssFile; | ||
| + | link.type = " | ||
| + | document.getElementsByTagName(' | ||
| + | } | ||
| + | } | ||
| + | cssLoad(" | ||
| + | </ | ||
| + | <script src=" | ||
| + | |||
| + | <!-- マップ表示領域 --> | ||
| + | <div id=" | ||
| + | |||
| + | < | ||
| + | //---- マップ表示 ---- | ||
| + | var view0 = new ol.View({center: | ||
| + | var map0 = new ol.Map({ | ||
| + | target: ' | ||
| + | layers: [new ol.layer.Tile({source: | ||
| + | view: view0, | ||
| + | controls: ol.control.defaults({attributionOptions: | ||
| + | }); | ||
| + | map0.addControl(new ol.control.ZoomSlider()); | ||
| + | |||
| + | //---- マーカーアイコン関連 ---- | ||
| + | // | ||
| + | var icontype0=[]; | ||
| + | // | ||
| + | icontype0[0]=new ol.style.Style({ | ||
| + | image: new ol.style.Icon(({ | ||
| + | anchor: | ||
| + | crossOrigin: | ||
| + | src: ' | ||
| + | })) | ||
| + | }); | ||
| + | // | ||
| + | icontype0[1]=new ol.style.Style({ | ||
| + | image: new ol.style.Icon(({ | ||
| + | anchor: | ||
| + | crossOrigin: | ||
| + | src: ' | ||
| + | })) | ||
| + | }); | ||
| + | |||
| + | |||
| + | //---- マーカー関連 ---- | ||
| + | var marker0=[]; | ||
| + | // | ||
| + | marker0[0] = new ol.Feature({ | ||
| + | geometry: new ol.geom.Point(ol.proj.fromLonLat([139.692, | ||
| + | }); | ||
| + | // | ||
| + | marker0[0].setStyle(icontype0[0]); | ||
| + | map0.addLayer(new ol.layer.Vector({source: | ||
| + | features: [marker0[0]] | ||
| + | })})); | ||
| + | // | ||
| + | marker0[1] = new ol.Feature({ | ||
| + | geometry: new ol.geom.Point(ol.proj.fromLonLat([140.122, | ||
| + | }); | ||
| + | // | ||
| + | marker0[1].setStyle(icontype0[1]); | ||
| + | map0.addLayer(new ol.layer.Vector({source: | ||
| + | features: [marker0[1]] | ||
| + | })})); | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== ソースコード ===== | ||
| + | <code javascript> | ||
| + | < | ||
| + | .mapdiv{height: | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | /* 外部CSSの読み込み */ | ||
| + | function cssLoad(cssFile){ | ||
| + | if(document.all){ | ||
| + | document.createStyleSheet(cssFile); | ||
| + | }else{ | ||
| + | var link = document.createElement(" | ||
| + | link.rel = " | ||
| + | link.href = cssFile; | ||
| + | link.type = " | ||
| + | document.getElementsByTagName(' | ||
| + | } | ||
| + | } | ||
| + | cssLoad(" | ||
| + | </ | ||
| + | <script src=" | ||
| + | |||
| + | <!-- マップ表示領域 --> | ||
| + | <div id=" | ||
| + | |||
| + | < | ||
| + | //---- マップ表示 ---- | ||
| + | var view0 = new ol.View({center: | ||
| + | var map0 = new ol.Map({ | ||
| + | target: ' | ||
| + | layers: [new ol.layer.Tile({source: | ||
| + | view: view0, | ||
| + | controls: ol.control.defaults({attributionOptions: | ||
| + | }); | ||
| + | map0.addControl(new ol.control.ZoomSlider()); | ||
| + | |||
| + | //---- マーカーアイコン関連 ---- | ||
| + | // | ||
| + | var icontype0=[]; | ||
| + | // | ||
| + | icontype0[0]=new ol.style.Style({ | ||
| + | image: new ol.style.Icon(({ | ||
| + | anchor: | ||
| + | crossOrigin: | ||
| + | src: ' | ||
| + | })) | ||
| + | }); | ||
| + | // | ||
| + | icontype0[1]=new ol.style.Style({ | ||
| + | image: new ol.style.Icon(({ | ||
| + | anchor: | ||
| + | crossOrigin: | ||
| + | src: ' | ||
| + | })) | ||
| + | }); | ||
| + | |||
| + | |||
| + | //---- マーカー関連 ---- | ||
| + | var marker0=[]; | ||
| + | // | ||
| + | marker0[0] = new ol.Feature({ | ||
| + | geometry: new ol.geom.Point(ol.proj.fromLonLat([139.692, | ||
| + | }); | ||
| + | // | ||
| + | marker0[0].setStyle(icontype0[0]); | ||
| + | map0.addLayer(new ol.layer.Vector({source: | ||
| + | features: [marker0[0]] | ||
| + | })})); | ||
| + | // | ||
| + | marker0[1] = new ol.Feature({ | ||
| + | geometry: new ol.geom.Point(ol.proj.fromLonLat([140.122, | ||
| + | }); | ||
| + | // | ||
| + | marker0[1].setStyle(icontype0[1]); | ||
| + | map0.addLayer(new ol.layer.Vector({source: | ||
| + | features: [marker0[1]] | ||
| + | })})); | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== 備考 ===== | ||
| + | アイコン以外の部分は[[osm4_ol_marker]]を見てください。 | ||
| + | |||
| + | 1.アイコンの設定を変数に格納します。 | ||
| + | <code javascript> | ||
| + | var icontype0=[]; | ||
| + | // | ||
| + | icontype0[0]=new ol.style.Style({ | ||
| + | image: new ol.style.Icon(({ | ||
| + | anchor: | ||
| + | crossOrigin: | ||
| + | src: ' | ||
| + | })) | ||
| + | }); | ||
| + | </ | ||
| + | オプションの説明 | ||
| + | ^anchor|アイコンの中心位置(地図座標と連結する場所)、[0, | ||
| + | ^crossOrigin|セキュリティー用:外部サーバーから画像を取得する時' | ||
| + | ^sec: | ||
| + | その他は[[https:// | ||
| + | |||
| + | 2.座標設定したマーカーにアイコンをセット | ||
| + | <code javascript> | ||
| + | // | ||
| + | marker0[0].setStyle(icontype0[0]); | ||
| + | </ | ||
| + | [[start]]>[[programming_start]] | ||
