Web: Difference between revisions

From Maria GDK Wiki
Jump to navigation Jump to search
No edit summary
()
Line 22: Line 22:
</source>
</source>


=== Minimal React Example ===
== Minimal React Example ==
The following example renders the map component with a WMS-map layer, which is one of many available map layers.
The following example renders the map component with a WMS-map layer, which is one of many available map layers.
It also includes a map layer selector, which is utilizing that all children of the map component automatically is populated with and can use the maria map api.
It also includes a map layer selector, which is utilizing that all children of the map component automatically is populated with and can use the maria map api.
<br>
You can choose to include this in your own project, or start from scratch using Create React App. We will show the latter here.
=== Create React App ===
Create React App is a simple way to initialize a new single page application using react. We will also use the typescript option included, to automatically set up the project to use typescript.
The first thing you need is to install NPM (node package manager), see [https://docs.npmjs.com/downloading-and-installing-node-js-and-npm Install Node and NPM].
To create the new react app called my-maria-map, navigate to the desired folder an run:
<source lang="bash">
npx create-react-app my-maria-map --template typescript
</source>
=== Add Maria map components ===
Navigate to my-maria-map/src/App.tsx, and change the content to the following:
<source lang="javascript">
<source lang="javascript">
import React, { useEffect } from "react";
import React, { useEffect } from "react";
Line 72: Line 87:
</source>
</source>


The MariaMap component will fill its parent, so remember to set the size of the parent component large enough.  
The MariaMap component will fill its parent, so we need to remember to set the size of the parent component large enough.  
For example:
In our examlpe, the maria-map-component is hosted by a <div> with id #root, so add the following to my-maria-map/src/index.css:
<source lang="css">
<source lang="css">
#parent-id{
#root{
   width: 100%;
   width: 100%;
   height: 100vh;
   height: 100vh;

Revision as of 15:51, 20 October 2021

Getting Started

Maria web map components can be used as standard Web Components, and as React components. An access token from Teleplan is needed to access these NPM-packages. The access token can be placed in a local .npmrc file in the root of the project using the packages.

Example .npmrc:

//registry.npmjs.org/:_authToken=YOUR_ACCESS_TOKEN

The .npmrc file should not be checked in to version control.

Web Components

To install the Web Component packages, install the following NPM-packages.

npm i @mariateleplan/map-core @mariateleplan/map-components

To be continued...

React Components

To install the react component packages, install the following NPM-packages.

npm i @mariateleplan/map-core @mariateleplan/map-react-components

Minimal React Example

The following example renders the map component with a WMS-map layer, which is one of many available map layers. It also includes a map layer selector, which is utilizing that all children of the map component automatically is populated with and can use the maria map api.
You can choose to include this in your own project, or start from scratch using Create React App. We will show the latter here.

Create React App

Create React App is a simple way to initialize a new single page application using react. We will also use the typescript option included, to automatically set up the project to use typescript. The first thing you need is to install NPM (node package manager), see Install Node and NPM.


To create the new react app called my-maria-map, navigate to the desired folder an run:

npx create-react-app my-maria-map --template typescript

Add Maria map components

Navigate to my-maria-map/src/App.tsx, and change the content to the following:

import React, { useEffect } from "react";

import { MariaMap, MapLayerSelect } from '@mariateleplan/map-react-components';
import { Maria as MariaCore} from '@mariateleplan/map-core/dist/Maria';
import {  WMSLayer } from '@mariateleplan/map-core/dist/layers';



let maria=new MariaCore({
  "viewState": {
    "zoom": 10,
    "center": [1197567, 8380058], // Oslo
    "projection": 'EPSG:3857'
  },
  "layersState": {
  }
});


function addMapLayers():void{

  const wmsLayer = new WMSLayer({      
    Id:"wms_test",
    Name:"WMS Raster",
    WmsUrl:new URL('https://ahocevar.com/geoserver/wms'),
    Layers:'ne:NE1_HR_LC_SR_W_DR'
  });
  maria.Layer.addLayer(wmsLayer); 

}

function App() {
  
  useEffect(() => {
      addMapLayers();
  }, [])

  return (
      <MariaMap MariaApi={maria}>
        <MapLayerSelect></MapLayerSelect>
      </MariaMap>
  );
}
export default App;

The MariaMap component will fill its parent, so we need to remember to set the size of the parent component large enough.

In our examlpe, the maria-map-component is hosted by a

with id #root, so add the following to my-maria-map/src/index.css:
#root{
  width: 100%;
  height: 100vh;
  background: rgb(64,64,64);
  overflow: hidden;
}