【发布时间】:2021-06-08 08:13:56
【问题描述】:
我正在尝试将列表与传单地图链接。我想单击结果卡并查看地图上的标记。我现在使用的是 json 格式的假数据。我想要一个像airbnb这样的显示,列表加上在地图上显示列表。
这是地图文件:
import React, { useState } from "react";
import { Map, TileLayer, Marker, Popup } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import { Icon } from "leaflet";
import FakeData from "../../../../../../data";
import {Div, PopContainer} from "./style"
const MarkerIcon = new Icon({
iconUrl: "../public/markerIcon.png",
iconSize: [50, 50],
});
function MapBox() {
const [nurse, setNurse] = useState(null);
const bull = <span>•</span>;
return (
<Div>
<Map center={[50.6365654, 3.0635282]} zoom={13} scrollWheelZoom={false}>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png"
/>
{FakeData.map((data) => (
<Marker
key={data.id}
icon={MarkerIcon}
position={[data.coordonatelat, data.coordonatelng]}
onmouseover={() => {
setNurse(data);
}}
/>
))}
{nurse && (
<Popup
position={[nurse.coordonatelat, nurse.coordonatelng]}
onClose={() => {
setNurse(null);
}}
>
<PopContainer>
<h2>
{bull}
{nurse.first_name} {nurse.last_name}
{bull}
</h2>
<h3>
{nurse.job}
</h3>
<p> {nurse.address}
<br />
{nurse.phone_number}
</p>
</PopContainer>
</Popup>
)}
</Map>
</Div>
);
}
export default MapBox;
这是结果页面:
import React from "react";
import FakeData from "../../../../../../data";
import MapBox from "../map/index";
import { Scrollbars } from "react-custom-scrollbars";
import {
Container,
H1,
Card,
CardContent,
H2,
Address,
Phone,
NavLink,
CardActions,
Job,
MapContainer,
BigContainer,
CardContainer,
Box,
Li,
} from "./resultPageStyle";
function Result(props) {
const bull = <span>•</span>;
return (
<BigContainer>
<Container>
<H1>Résultats</H1>
<Box>
<Scrollbars style={{ width: 650, height: 580 }}>
<CardContainer>
{FakeData.slice(0, 30).map((item) => (
<Li key={item.id}>
<Card>
<CardContent>
<Job>{item.job}</Job>
<H2>
{bull}
{item.first_name} {item.last_name}
{bull}
</H2>
<Address>{item.address}</Address>
<Phone>{item.phone_number}</Phone>
</CardContent>
<CardActions>
<NavLink to={"/detailsPage"}>Plus d'infos</NavLink>
</CardActions>
</Card>
</Li>
))}
</CardContainer>
</Scrollbars>
<MapContainer>
<MapBox />
</MapContainer>
</Box>
</Container>
</BigContainer>
);
}
export default Result;
我尝试了不同的东西,但它不起作用。
【问题讨论】:
-
你能提供一个小演示来玩吗?此代码确实有助于重现问题
-
codesandbox.io/s/floral-sunset-spit4?file=/src/… 但我在代码和框中收到错误
标签: reactjs leaflet react-leaflet