2

Problem: OpenLayers(v6.3) MAP is visible in REACTJS(16.13) but click event on MAP is not getting called.

Purpose: I want to place a marker and handle its click event to show some POPUP with data.

Code: Code included is though self explanatory; here is a quick explanation

[in ComponentWillUpdate()] MapComponent shown here receive {currentLocation} as property and moves map to that location. It then places a marker with custom Image. Its all working.

[in ComponentDidMount()] DIV ID reference is used to pass OLMAP reference.

I tried to register OLMAP click event in both ComponentDidMount() as well as ComponentWillUpdate() but it is not doing anything though it is getting executed as last console message gets printed.

Only problem in this code is OL-MAP CLICK event Handling. What is wrong. Help will be appreciated.

    import React, { Component } from 'react';
    import ReactDOM from 'react-dom';    
    import Map from 'ol/Map';
    import View from 'ol/View';
    import TileLayer from 'ol/layer/Tile';
    import OSM from 'ol/source/OSM';
    import VectorLayer from 'ol/layer/Vector';
    import VectorSource from 'ol/source/Vector';
    import Feature from 'ol/Feature';
    import {Style,Icon} from 'ol/style';
    import Point from 'ol/geom/Point';
    
    import { fromLonLat } from 'ol/proj.js';
    import LOCPNG from '../../../assets/images/location.png';
    class MapComponent extends Component {
    
      constructor(props) {
        // console.log('[MC.js] constructor');

    super(props);
    this.olMap = null;
    this.mapRef = null;
    this.setMapRef = element => {
      this.mapRef = element;
    }
  }
  componentDidUpdate(prevProps, prevState) {
    console.log('[MC.js] CDUpdate');
  }
  componentWillUpdate(nextProps) {
    // console.log('[MC.js] CWU');
    const mapView = this.olMap.getView();
    mapView.animate({
      center: nextProps.currentLocation,
      duration: 2000,
      zoom: 10
    });

    this.makeMarker(nextProps.currentLocation, 0);

    this.olMap.on('click',(evt) => {
      console.log(evt);
      console.log(evt.pixel);
      const feature = this.olMap.forEachFeatureAtPixel(evt.pixel, (feature) => feature);
      if(feature){
        console.log('PopUp');
      }
    });
  }
  makeMarker = (location, index) => {
    let marker = new Feature({
      geometry: new Point(location)
    });
    marker.setStyle(new Style({
      image: new Icon(({
        // crossOrigin: 'anonymous',
        src: LOCPNG,
        // enter code here`enter code here`
      }))
    }));
    let vectorSource = new VectorSource({ features: [marker] })
    var markerVectorLayer = new VectorLayer({
      source: vectorSource,
    });
    this.olMap.addLayer(markerVectorLayer);
  }

  componentDidMount() {
    // console.log('[MC.js] CDM');
    const mapDOMNode = ReactDOM.findDOMNode(this.mapRef);
    // console.log('[MC.js] CDM',mapDOMNode);
    this.olMap = new Map({
      target: mapDOMNode,
      layers: [
        new TileLayer({
          source: new OSM()
        })
      ],
      view: new View({
        center: this.props.currentLocation,
        zoom: 10
      })
    });

    this.olMap.on('click',evt=>{
      console.log(evt);
      console.log(evt.pixel);
      const feature = this.olMap.forEachFeatureAtPixel(evt.pixel, (feature) => feature);
      if(feature){
        console.log('PopUp');
      }
    });


    console.log('[MC.js] CDM');
  }

  render() {
    // console.log('[MC.js] render()');
    const styles = { height: '100%', width: '100%' }
    return (
      <React.Fragment>
        <div style={styles} ref={this.setMapRef}></div>
      </React.Fragment>
    )
  }
}

export default MapComponent;
u tyagi
  • 732
  • 9
  • 18
  • have you included the OpenLayers css? – Mike Jul 03 '20 at 09:36
  • Yes. Ol.css included in public/index.html. everything working like moving to {currentlocation}, Map visibility. Just map.on('click', ()=>{}) not getting called. – u tyagi Jul 03 '20 at 14:03
  • @Mike Can you explain, what's the difference between 1) importing as explained in above comment and 2) importing in specific JS file like import 'ol/ol.css'; Later has resolved the issue while 1) was failure. – u tyagi Jul 08 '20 at 08:05
  • @Mike: Can you post that answer and explain my query? – u tyagi Aug 04 '20 at 11:34
  • If import 'ol.css' did not work (were the zoom buttons correctly styled?) it was probably a react configuration issue. Versions 6.2 and 6.3 of OpenLayers required the css to style divs to make them responsive. That has now been fixed in 6.4 https://github.com/openlayers/openlayers/pull/10936 – Mike Aug 04 '20 at 13:36
  • I have the same problem, using React + Material UI. The map is inside a Box – Juan Salvador Nov 15 '20 at 16:10

0 Answers0