Angular で開発したプロジェクトにマップを読み込もうとしています。そのために、私は OpenLayers を使用しています。
タイルをダウンロードしたいのリポジトリは
http://www.ign.es/wmts/mapa-raster
です。
このサイトにアクセスすると、
WMTS のテスト リクエスト
が実行され
ます。このリクエストを使用して、サーバーの応答とリクエストのテンプレートをテストできます。
- 層=MTN
- スタイル = デフォルト
- TileMatrixSet = EPSG:4326
- サービス = WMTS
- フォーマット = 画像/png
- TileMatrix = EPSG:4326:4
サーバーは正常に応答しましたが、OpenLayer ライブラリを使用してこのオプションを Angular コンポーネントに配置すると、ページには何も表示されませんが、XYZ などの他のサービスを使用すると、完全に機能します。残念ながら、このプロジェクトには WMTS サービスが必要です。
これは私のコードです:
ol-map.component.ts
import { Component, OnInit,AfterViewInit,Input,ElementRef } from '@angular/core';
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import VectorLayer from 'ol/layer/Vector';
import XYZ from 'ol/source/XYZ';
import WMTS from 'ol/source/WMTS'
import WMTSTileGrid from 'ol/tilegrid/WMTS';
import {getTopLeft} from 'ol/extent'
import { OSM} from 'ol/source';
import { Tile} from 'ol/layer';
import * as Proj from 'ol/proj';
import {
defaults as defaultControls,
Control,
ScaleLine,
ZoomSlider
} from 'ol/control';
import {fromLonLat, get as getProjection} from 'ol/proj';
import {getWidth} from 'ol/extent';
export const DEFAULT_HEIGHT = '100%';
export const DEFAULT_WIDTH = '100%';
export const DEFAULT_LAT = 39;
export const DEFAULT_LON = -7;
@Component({
selector: 'ol-map',
templateUrl: './ol-map.component.html',
styleUrls: ['./ol-map.component.css']
})
export class OlMapComponent implements OnInit,AfterViewInit {
@Input() lat: number = DEFAULT_LAT;
@Input() lon: number = DEFAULT_LON;
@Input() zoom: number = 10;
@Input() width: string | number = DEFAULT_WIDTH;
@Input() height: string | number = DEFAULT_HEIGHT;
map: Map;
private mapEl: HTMLElement;
resolutions = [];
matrixIds = [];
proj4326 = getProjection('EPSG:4326');
maxResolution = getWidth(this.proj4326.getExtent()) / 256;
constructor(private elementRef: ElementRef) {
}
tileGrid:WMTSTileGrid;
ngAfterViewInit(): void {
for (var z = 0; z < 18; ++z) {
// generate resolutions and matrixIds arrays for this WMTS
this.resolutions[z] = this.maxResolution / Math.pow(2, z);
this.matrixIds[z] = "EPSG:4326:" + z;
}
this.tileGrid = new WMTSTileGrid({
origin: getTopLeft(this.proj4326.getExtent()),
resolutions: this.resolutions,
matrixIds: this.matrixIds,
});
this.mapEl = this.elementRef.nativeElement.querySelector('#map');
this.setSize();
this.map = new Map({
target: 'map',
layers: [
new TileLayer({
opacity: 0.7,
extent: this.proj4326.getExtent(),
source: new WMTS({
url: 'http://www.ign.es/wmts/mapa-raster/',
layer: 'MTN',
matrixSet: 'EPSG:4326',
format: 'image/png',
projection: this.proj4326,
tileGrid: this.tileGrid,
style: 'default',
// wrapX: true,
attributions:
'Teselas de PNOA cedido por © Instituto Geográfico Nacional de España',
})
})
],
view: new View({
// projection: 'EPSG:4326',
center: Proj.fromLonLat([this.lon, this.lat]),
zoom: this.zoom
}),
controls: defaultControls().extend([
new ScaleLine({}),
new ZoomSlider
])
});
}
private setSize() {
if (this.mapEl) {
const styles = this.mapEl.style;
styles.height = coerceCssPixelValue(this.height) || DEFAULT_HEIGHT;
styles.width = coerceCssPixelValue(this.width) || DEFAULT_WIDTH;
}
}
ngOnInit(): void {
}
}
const cssUnitsPattern = /([A-Za-z%]+)$/;
function coerceCssPixelValue(value: any): string {
if (value == null) {
return '';
}
return cssUnitsPattern.test(value) ? value : `${value}px`;
}
ol-map.component.html
<div id="map" class="map"></div>
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>OpordEasyApp</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://openlayers.org/en/v6.1.1/css/ol.css" type="text/css">
</head>
<body class="mat-typography">
<app-root></app-root>
</body>
</html>