React Leaflet - 如何将弹出窗口居中显示在地图上

4
如果我点击一个标记,我希望将地图视图平移到标记的中心。到目前为止,这在我的项目中可以实现。此外,我希望打开的弹出窗口与地图视图的中心对齐。
我认为我可以使用“leaflet-popup”类硬编码来实现这一点,如下所示:
.leaflet-popup {
  transform: translate(-40%, 50%) !important;
}

但是这并不起作用。我的代码问题在于,弹出窗口的位置独立于地图视图。但它应该根据当前视图居中。我可以向您展示我的地图设置:

编辑

我提供了一个CodeSandBox链接供您玩耍。点击顶部的标记,您会发现弹出窗口未对齐到屏幕中心:

地图组件

const {Map, TileLayer, Marker, GeoJSON} = ReactLeaflet;

function MapOverlay({setSwipeState}) {
    const mapRef = useRef(null);
    const [donationLocations] = useState([
        [48.135125, 11.581981], [58.403, 20.420], [43.300, 40],
        [70.505, -20], [40.505, -80], [-40.505, -10]
    ]);


    function centerMapView(e) {
        const {leafletElement} = mapRef.current;

        if(e) {
            leafletElement.setView(e.popup._latlng); // center view to where popup opens
            // todo: align popup in the middle of the screen
            // Get bounds of map view, divide it by 2 and apply coorditanes to the popup position


        }

    }

    function getMapData() {
        return (
            <div>
                {
                    donationLocations.map((position, i) =>
                        (
                            <Marker position={position} key={i}>
                                <MarkerPopup/>
                            </Marker>
                        )
                    )
                }
            </div>
        )
    }

    return (
        <Map
            ref={mapRef}
            center={[45.000, 10.000]}
            zoom={3}
            onPopupopen={centerMapView.bind(this)}
            zoomControl={false}
            minZoom={3}
            bounceAtZoomLimits={true}
            maxBoundsViscosity={.95}
            maxBounds={[[-180, -90], [180, 90]]}
        >
            <TileLayer
                noWrap={true}
                attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors | &amp;copy <a href="https://apps.mapbox.com/feedback/">Mapbox</a>'
                url={'https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/{z}/{x}/{y}?access_token=' + process.env.REACT_APP_MAPBOX_KEY}
            />
            {getMapData()}
        </Map>
    )
}

标记弹出组件

const {Popup} = ReactLeaflet;

export default function MarkerPopup() {

    return (
        <Popup
            autoPan={false} // Important part here
        >
            <Card>
                   ...
            </Card>
        </Popup>
    );
}

你好。这是你目前的代码吗?如果是这样,你可以将标记在其y轴上居中,而不是在地图的中心,并且弹出窗口看起来也很好,也居中。你想要实现什么? - kboul
@kboul 我更新了我的问题,并提供了一个 CodeSandBox 链接。我想要实现的是一个完美对齐于地图中心的弹出窗口。问候 Marco - MarcoLe
3个回答

4
我已经成功使其工作。解决方案是创建一个与DOM分离的模态对话框。
你可以查看可工作的代码:这里
// Pass open state an setOpen function from the parent
function MarkerPopup({open, setOpen}) {
  const classes = useStyles();

  function handleClose() {
      setOpen(false);
  }

  return (
      <Popup
          autoPan={false}
      >
          <Modal
              className={classes.modal}
              open={open}
              classes={{root: classes.root}}
              onClose={handleClose.bind(this)}
              BackdropComponent={Backdrop}
              BackdropProps={{
                  timeout: 0,
                  invisible: true
              }}
          >
              <Card className={classes.card}>
                  <CardMedia
                      className={classes.media}
                      image="https://material-ui.com/static/images/cards/contemplative-reptile.jpg"
                      title="Contemplative Reptile"
                  />
                  <CardContent>
                      <Typography gutterBottom variant="h5" component="h2">
                          Lizard
                      </Typography>
                      <Typography variant="body2" color="textSecondary" component="p">
                          Lizards are a widespread group of squamate reptiles, with over 6,000 species, ranging
                          across all continents except Antarctica
                      </Typography>
                  </CardContent>
                  <CardActions>
                      <Button size="small" color="primary">
                          Details
                      </Button>
                      <Button size="small" color="primary">
                          Donate
                      </Button>
                  </CardActions>
              </Card>
          </Modal>
      </Popup>
  );
}

0
function centerMapView(e) {
  const { leafletElement } = mapRef.current;
  if (e) {
    const popupLatlng = e.popup._latlng;
    const zoom = leafletElement.getZoom();
    const point = leafletElement.project(popupLatlng, zoom);
    const newPoint = point.subtract([0, 180]);
    const newLatlng = leafletElement.unproject(newPoint, zoom);
    leafletElement.panTo(newLatlng, { animate: true });
  }
}

你可以将你的 centerMapView 函数更新为以下内容。这将首先将你的 Latlng 值转换为一个 point 值,然后通过减去特定数量的像素(偏移量)来修改该值,最后将 point 值转换回 Latlng 并使用该值调用 panTo

所有这些代码都更新地图视图容器。但是,如果标记例如位于加拿大北部并且弹出窗口打开,则弹出窗口不会放置在屏幕中央。请参见我的问题中的CodeSandBox链接。 - MarcoLe

0
最好的解决方案是制作一个模态弹出窗口,它始终位于屏幕中央。
在事件 on('popupopen') 上显示它。

好的,那么在这些坐标上添加一个标记:[-84,-99]。抱歉,但这不起作用。 - MarcoLe
1
然后只需创建一个模态弹出窗口。 - Jay Z

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接