npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-openlayers-styled-map

v0.0.9

Published

Openlayers map component wrapped in react component with style and global hooks

Downloads

11

Readme

react-openlayers-styled-map

Openlayers map component wrapped in react component with style and global hooks

NPM TypeScript Style Guide NPM

This (react-openlayers-styled-map) is a React.JS component made in Typescript. It acts as a wrapper around OpenLayers map object and tries to supress the complexity of a starting map project.

Currently, the lib has a simple global state hook that provides access from any part of a react project to the OL map object and some extra key functions.

Also, some usefull styled and ready-to-use map controls, some of them directly translated from OL Docs.

  • Measure Polygon area
  • Measure Distance
  • Export map as image (uses html2canvas)
  • Export map as PDF (uses jsPDF)
  • Draw Circle Radius
  • Pin Coordinates
  • Redirect to Google Street View from point

Install

npm install --save react-openlayers-styled-map
//or
yarn add react-openlayers-styled-map

//ATTENTION
//react-openlayers-styled-map uses Openlayers (ol) package as peer dependency
npm install ol
yarn add ol

Demo

In react-openlayers-styled-map is possible to test a working demo/project of the component and also a debug page to quick test some XYZ/WMS layer sources.

The page is also using routing from react-router-dom library to test the map component remounting capabilities and bugs.

Usage

Map Component

import React, { Component } from 'react';

import { StyledMap, Controls } from 'react-openlayers-styled-map';

const Page = () => {
  return (
    <StyledMap
      id='map' // Optional, just in case of conflict with other components
      startCoordinates={[-49.2, -26.5]}
      startZoom={11}
      tileDebug // Enable tile debug overlay for testing
      osmBasemap //Enable OSM Background for quick testing
      defaultControls={{
        /*
                You can leave {} to enable the default props for each control
                See Openlayers Specific control documentation for custom properties
                */
        fullScreenMode: { tipLabel: 'Click to toggle' },
        zoomButtons: {},
        zoomSlider: {},
        scale: {
          bar: true,
          minWidth: 130,
          steps: 4,
        },
      }}
    >
      <StyledMap.Controls showRibbon>
        <Controls.GoogleStreetView styled activeLabel='custom label' />
        <Controls.ExportMapImage styled color='purple' />
        <Controls.ExportMapPDF styled />
        <Controls.MeasureArea styled />
        <Controls.MeasureDistance styled />
        <Controls.MeasureRadius styled />
        <Controls.ClearMeasures styled />
        <Controls.PinCoordinates styled />
      </StyledMap.Controls>
    </StyledMap>
  );
};

Hooks / Methods Abstractions

Access map object


import { useMap } from 'react-openlayers-styled-map'
const Sidebar = () => {
  const { map } = useMap();
  //map is the pure ol map object, so you can access all the power of the OpenLayers lib
  useEffect(()=>{
    map.addOverlay(...)
    map.addInteraction(...)
    map.addLayer(...)
    map.on('click',()=>{})
  },[])
  return <div>My Sidebar Component</div>
}

Abstractions that comes with the library


import { useMap } from 'react-openlayers-styled-map'
const Component = () => {
  const = {
    getLayer,
    addLayer,
    removeLayer,
    setActiveMenuControl
    } = useMap();


  setActiveMenuControl('GoogleStreetView') // Remotely enable menu control
  setActiveMenuControl(undefined) // Disable all menu controls, this method triggers the onDisable method on the currently active control

  //addLayer method wraps around map.addLayer and register the objects in the activeLayers
  // for and easy to use access for custom management components Ex: like Layer Selectors or TreeViews
  addLayer({
    layerKey: 'layerUniqueName2',
    layerObject:new TileLayer({source: new OSM()})
    });
  console.log(getLayer('layerUniqueName2'))
  // {layerKey:'layerUniqueName2',layerObject:object}

  //remove layer from map
  removeLayer('layerUniqueName2')
}

Custom Menu Control

You can create a new custom control just wrapping the default ControlButton component and point the enable/disable callbacks and a unique key.

import React, { useCallback } from 'react';
import { Controls, useMap } from 'react-openlayers-styled-map';
import { FaCrosshairs } from 'react-icons/fa';
import { toLonLat } from 'ol/proj';
import { MapBrowserEvent } from 'ol';

const MyCustomControl: React.FC = () => {
  const { map } = useMap();

  const onMapClick = (e: MapBrowserEvent): void => {
    const coords = toLonLat(e.coordinate);
    alert(coords);
  };

  const onEnable = useCallback(() => {
    map.getViewport().style.cursor = 'crosshair';
    map.on('click', onMapClick);
  }, [map]);

  const onDisable = useCallback(() => {
    map.getViewport().style.cursor = '';
    map.un('click', onMapClick);
  }, [map]);

  return (
    <Controls.ControlButton
      styled
      icon={<FaCrosshairs size={20} color='#fff' />}
      activeLabel='Print Map Coordinates'
      color='#FE2C54'
      enable={onEnable}
      disable={onDisable}
      controlKey='PrintMapCoordinates'
    />
  );
};

Usage

<StyledMap {...ANOTHER PROPS}>
    <StyledMap.Controls showRibbon >
      <MyCustomControl styled />
    </StyledMap.Controls>
</StyledMap>

Styled and Default Styled

Each control component has a styled prop to enable the styled interface.

License

MIT © cassianorsd