dayjournal memo

Total 975 articles!!

OpenLayers #004 – ライン表示

Yasunori Kirimoto's avatar


画像



ラインを表示するメモ。



画像



script.js

import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import { fromLonLat } from 'ol/proj';

// 各ライブラリ読み込み
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON';
import Style from 'ol/style/Style';
import Stroke from 'ol/style/Stroke';


// ラインGeoJSON設定
const geojsonObject = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [139.75278854370114, 35.66413037753069],
                    [139.76523399353027, 35.67214927327908],
                    [139.77107048034668, 35.68888176518044],
                    [139.76660728454587, 35.69703758268826]
                ]
            }
        }
    ]
};

// ラインレイヤ設定
const vectorLayer = new VectorLayer({
    source: new VectorSource({
        features: new GeoJSON({
            featureProjection: 'EPSG:3857'
        }).readFeatures(geojsonObject)
    }),
    style: new Style({
        stroke: new Stroke({
            color: 'rgba(255, 0, 0, 0.6)',
            width: 5
        })
    })
});


// MIERUNE MONO読み込み
const map = new Map ({
    target: 'map',
    layers: [
        new TileLayer({
            source: new XYZ({
                url: 'https://tile.mierune.co.jp/mierune_mono/{z}/{x}/{y}.png',
                attributions: 'Maptiles by <a href="http://mierune.co.jp" target="_blank">MIERUNE</a>, under CC BY. Data by <a href="http://osm.org/copyright" target="_blank">OpenStreetMap</a> contributors, under ODbL.',
                attributionsCollapsible: false,
                tileSize: [256, 256],
                minZoom: 0,
                maxZoom: 18
            })
        }),
        // ラインレイヤ指定
        vectorLayer
    ],
    view: new View ({
        center: fromLonLat([139.767, 35.681]),
        zoom: 14
    })
});



OpenLayersを手軽に始めるビルド環境公開しています。
openlayers-starter



book

Q&A