Openlayers amb ReactJS

De wikijoan
Salta a la navegació Salta a la cerca

Exemples que funcionen

Exemple 1

url original:

explicació a la wiki:

Exemple 2


  • ol: 4.6.5
  • react: 16.3.2
  • react-dom: 16.3.2
  • react-scripts: 1.1.4
$ npm install (hem d'haver creat prèviament el ''package.json'' que hi ha en el projecte)
$ npm install ol --save
$ npm install react --save
$ npm install react-dom --save
$ npm install react-scripts --save

$ npm start


import React from "react";
import { render } from "react-dom";
import MyMap from "./MyMap";

const App = () => (
    <MyMap />

render(<App />, document.getElementById("root"));


import React, { Component } from "react";
import OlMap from "ol/map";
import OlView from "ol/view";
import OlLayerTile from "ol/layer/tile";
import OlSourceOSM from "ol/source/osm";

class PublicMap extends Component {
  constructor(props) {

    this.state = { center: [0, 0], zoom: 1 };

    this.olmap = new OlMap({
      target: null,
      layers: [
        new OlLayerTile({
          source: new OlSourceOSM()
      view: new OlView({
        zoom: this.state.zoom

  updateMap() {

  componentDidMount() {

    // Listen to map changes
    this.olmap.on("moveend", () => {
      let center = this.olmap.getView().getCenter();
      let zoom = this.olmap.getView().getZoom();
      this.setState({ center, zoom });

  shouldComponentUpdate(nextProps, nextState) {
    let center = this.olmap.getView().getCenter();
    let zoom = this.olmap.getView().getZoom();
    if (center === && zoom === nextState.zoom) return false;
    return true;

  userAction() {
    this.setState({ center: [546000, 6868000], zoom: 5 });

  render() {
    this.updateMap(); // Update map on render?
    return (
      <div id="map" style={{ width: "100%", height: "360px" }}>
        <button onClick={e => this.userAction()}>setState on click</button>

export default PublicMap;


<!DOCTYPE html>
<html lang="en">

	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<meta name="theme-color" content="#000000">
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See
	<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
	<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
	<title>React App</title>

		You need to enable JavaScript to run this app.
	<div id="root"></div>
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.


Exemple 3: react-openlayers

A minimal React wrapper of OpenLayers 3+ written in TypeScript

Instal·lo les dependències:

$ npm install react-openlayers --save-dev

Descarrego el projecte i l'instal·lo:

$ git clone
$ cd react-openlayers
$ npm install
$ npm start

i funciona:

És un codi que té moltes opcions

creat per Joan Quintana Compte, abril 2020