反应地图传单circleMarker不渲染

时间:2019-10-30 10:51:27

标签: reactjs leaflet marker

我有一张传单地图,我有一个按钮。我想在单击按钮时显示一些circleMarker,然后在再次单击时删除circleMarker(尚未实现删除选项) 当我单击按钮时,我不想每次渲染所有地图,而只希望渲染circleMarker。 CircleMarker不会在地图上呈现,但是我可以看到地图

这是我的代码:

组件图

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)

1 个答案:

答案 0 :(得分:0)

除非您使用不使用的CircleMarker,否则传单库中没有公开的react-leaflet组件。因此,以下是您需要使用的代码,以将其添加到地图圆圈标记中:

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);
    });
};

为了简单起见,我删除了if语句。

Demo