Wil*_*ard 3 javascript mapbox reactjs mapbox-gl-js next.js
如何在Mapbox 地图对象之外创建可以拖入其中的元素?例如,假设我想在页面上呈现位置列表。每个位置都是一个带有自定义标记或图标的 React 组件。
在此位置列表旁边是 Mapbox 地图。位置列表不会在地图内呈现。虽然我知道可以使这些单独的位置组件可拖动,但是否可以将它们拖放到 Mapbox 地图中,并将其识别为地图上具有纬度/经度坐标的实际标记?如果是这样,我该怎么做?
以下是我尝试过的代码中的相关源文件:
索引.js
import dynamic from "next/dynamic";
import { useSelector } from "react-redux";
import Plant from "../../components/Plant";
const MapboxMap = dynamic(() => import("../../components/MapboxGLMap"), {
ssr: false,
});
const Blueprint = () => {
const plants = useSelector((state) => state.plants);
const showPlants = () => {
return (
<React.Fragment>
{plants.map((plant) => (
<Plant plant={plant} />
))}
</React.Fragment>
);
};
return (
<React.Fragment>
<div className="ui container centered grid blueprint">
<div className="three wide column scrollable">
<div className="ui link one cards">{showPlants()}</div>
</div>
<div className="twelve wide column">
<MapboxMap />
</div>
<style jsx>{`
.scrollable {
height: calc(100vh);
overflow-x: auto;
}
`}</style>
</React.Fragment>
);
};
export default Blueprint;
Run Code Online (Sandbox Code Playgroud)
Plant.jsx
import React from "react";
import { useDrag } from "react-dnd";
const ItemTypes = {
PLANT: "plant",
};
const Plant = ({ plant }) => {
const [{ isDragging }, drag] = useDrag({
item: { type: ItemTypes.PLANT },
collect: (monitor) => ({
isDragging: !!monitor.isDragging(),
}),
});
return (
<div
ref={drag}
style={{
opacity: isDragging ? 0.1 : 1,
cursor: "move",
}}
key={plant.id}
className="card"
>
<div className="image">
<img src="/white-image.png" />
</div>
<div className="content">
<div className="center aligned">{plant.common_name}</div>
</div>
</div>
);
};
export default Plant;
Run Code Online (Sandbox Code Playgroud)
MapboxGLMap.jsx
import React, { useEffect, useRef, useState } from "react";
import mapboxgl from "mapbox-gl";
import MapboxGeocoder from "@mapbox/mapbox-gl-geocoder";
import MapboxDraw from "@mapbox/mapbox-gl-draw";
const MAPBOX_TOKEN = "xxx";
const styles = {
width: "100%",
height: "100%",
position: "absolute",
};
const MapboxGLMap = () => {
const [map, setMap] = useState(null);
const [lng, setLng] = useState(null);
const [lat, setLat] = useState(null);
const [plant, setPlant] = useState(null);
const mapContainer = useRef(null);
useEffect(() => {
mapboxgl.accessToken = MAPBOX_TOKEN;
const initializeMap = ({ setMap, mapContainer }) => {
const map = new mapboxgl.Map({
container: mapContainer.current,
style: "mapbox://styles/mapbox/satellite-v9", // stylesheet location
center: [0, 0],
zoom: 5,
});
map.on("load", () => {
setMap(map);
map.resize();
});
map.on("click", (e) => {});
map.addControl(
new MapboxGeocoder({
accessToken: MAPBOX_TOKEN,
mapboxgl: mapboxgl,
})
);
const draw = new MapboxDraw({
displayControlsDefault: false,
controls: {
polygon: true,
trash: true,
},
});
map.addControl(draw);
map.on("draw.create", (e) => {
console.log("e =>", e);
console.log("draw.getAll()", draw.getAll());
});
map.on("mousemove", (e) => {
// console.log(e.point);
setLng(e.lngLat.wrap().lng);
setLat(e.lngLat.wrap().lat);
});
};
if (!map) initializeMap({ setMap, mapContainer });
}, [map]);
return <div ref={(el) => (mapContainer.current = el)} style={styles} />;
};
export default MapboxGLMap;
Run Code Online (Sandbox Code Playgroud)
实际上,根据您的相关标签,我想您想将诸如pin 之类的东西从外部拖放到地图区域。你使用reactjs标签,这意味着你想通过使用 ReactJS 来做到这一点。
为此,您应该通过 npm 或 yarn 安装 Mapbox:
npm install mapbox-gl --save
Run Code Online (Sandbox Code Playgroud)
或者
yarn add mapbox-gl
Run Code Online (Sandbox Code Playgroud)
然后您应该将 Mapbox 区域包裹在放置区中。为此,您可以使用react-dropzone. 通过以下命令安装它:
npm install react-dropzone --save
Run Code Online (Sandbox Code Playgroud)
或者
yarn add react-dropzone
Run Code Online (Sandbox Code Playgroud)
将以下行添加到 HTML 模板:
npm install mapbox-gl --save
Run Code Online (Sandbox Code Playgroud)
然后像下面这样使用它:
yarn add mapbox-gl
Run Code Online (Sandbox Code Playgroud)
通过使用此方法,您可以放置一些图像并获取它并根据放置位置将其显示在地图上。
注意:将捕获的文件类型减少到图像文件类型,如jpg/png
| 归档时间: |
|
| 查看次数: |
1185 次 |
| 最近记录: |