1

I have a leaflet map, and I have one button. I want to display some circlesMarker when I click on a button and remove the circlesMarker when I click again (remove option is not implemented yet) I don't want to render each time all map when I click the button, I want render only the circleMarker. CircleMarker doesn't render on a map, but I can see the map

Here my code :

COMPONENT MAP

function Map1() {

    const apartments = [
        { roomType: 'shared room', geometry: [41.402610, 2.204270] },
        { roomType: 'shared room', geometry: [41.411300, 2.217630] },
        { roomType: 'private room', geometry: [41.410220, 2.212520] },
        { roomType: 'apartament sencer', geometry: [41.410630, 2.216970] },
        { roomType: 'private room', geometry: [41.409190, 2.209030] },
    ]

    let map = useRef(null);
    useEffect(() => {
        let url = 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}'
        const access = 'pk.eyJ1Ijoiam9zZXQyMSIsImEiOiJjazF1bGZlcHowYjVlM2RwYW9ia2pwaWtlIn0.9n-6tKArfdSfd15Do6YxLA'
        map.current = L.map("map");
        const defaultCenter = [41.383, 2.173];
        const defaultZoom = 13.10;

        let layer = L.tileLayer(url, {
            maxZoom: 18,
            id: 'mapbox.streets',
            accessToken: access,
        });

        map.current.setView(defaultCenter, defaultZoom);
        layer.addTo(map.current);
}, [map])

const handleOnclik = (e) => {
        e.preventDefault()
        let color = (e.target.name)

        if (color === 'pink') {
            apartments.map(item => {
            if (item.roomType === 'private room' || item.roomType === 'shared room') {
               return (
                        <div>
                            <p>Carme</p>
                                <CircleMarker className="circle"
                                    center={[41.409190, 2.209030]}
                                    color={'#000080'}
                                    width={.5}
                                    fillColor={'blue'}
                                    fillOpacity={0.5}
                                    stroke={'black'}
                                >
                                </CircleMarker >
                        </div>
                    )
                }
            })
        } 
return (<>
        <div className="container">
            <div>
                <div id="map" className="normalscreen"></div>
            </div>
            <div id="basemaps-wrapper" className="leaflet-bar">
                <select id="basemaps">
                    <option>Visualització</option>
                    <option value="mapbox.streets">Streets</option>
                    <option value="mapbox.satellite">Satellite</option>
                    <option value="mapbox.outdoors">Outdoors</option>
                    <option value="mapbox.dark">Dark</option>
                    <option value="mapbox.light">Light</option>
                    <option value="mapbox.DarkGray">Dark Gray</option>
                </select>
                <button onClick={onChangeFullScreen}>Full Screen</button>
                <div>
                    <img onClick={handleOnclik} name="pink" src="images/pink" alt="habitacio" />
                    <img onClick={handleOnclik} name="green" src="images/green" alt="apartament" />
                </div>
            </div>
        </div>
    </>)
}
export default Map1

MAP CSS

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

.container {
  display: flex;
  flex-direction: column;
  width: 100vw;
  justify-content: center;
}

#map {
  display: block;
}

.normalscreen {
  width: 50%;
  height: 50vh;
  left: 49%;
  position: relative;
}

.fullscreen {
  width: 100%;
  height: 80vh;
  left: 0%;
  text-align: center;
}

APP

import React from 'react'
import { withRouter } from 'react-router-dom'
import Navbar from './Navbar'
import Map from './Map'

function App() {

  return (<>
    <Navbar />
    <Map />

  </>);
}
export default withRouter(App)
5
  • Did you see any errors in the browser console? Also, did you see if the component is loaded/appearing using React Dev tools browser plugin? Also, can you put the code of CircleMarker component? Commented Oct 30, 2019 at 10:55
  • I don't have any error in the browser, and the component circleMarker doesn't load/appear.The code the circleMarker is inside of the return : <CircleMarker className="circle" center={[41.409190, 2.209030]} color={'#000080'} width={.5} fillColor={'blue'} fillOpacity={0.5} stroke={'black'} > </CircleMarker >
    – ccnat
    Commented Oct 30, 2019 at 11:21
  • I mean the source code of CircleMarker component. Inside the return you are just calling it to render. Commented Oct 30, 2019 at 11:22
  • But I have a function handleOnclick and inside I have the <circleMarker> and I can't render and return in react... only render. I think in Class I can render and return, but I need a function
    – ccnat
    Commented Oct 30, 2019 at 11:25
  • 1
    ccnat please check my answer and let me know if it answers your issue
    – kboul
    Commented Oct 30, 2019 at 12:21

1 Answer 1

0

There is no CircleMarker component on leaflet library exposed unless you use react-leaflet which you don't. Therefore here is the code you need to use in order to add to the map circle markers:

const handleOnclik = () => {
    apartments.forEach(({ geometry }) => {
      new L.CircleMarker(geometry, {
        radius: 5,
        fillColor: "blue",
        width: 0.5,
        stroke: "black",
        color: "#000080",
        fillOpacity: 0.5
      }).addTo(map.current);
    });
};

I removed the if statements for simplicity just to illustrate an example.

Demo

Not the answer you're looking for? Browse other questions tagged or ask your own question.