react-leaflet - 无法使用 JSON 变量设置 React-Leafletlet Maps CRS
问题描述
我无法使用以下代码为 React-Leaflet 设置 crs:
import CONFIG from 'config/config.json';
.
.
.
.
<Map
ref={(m) => { this.leafletMap = m; }}
center={this.props.mapCenter}
zoom={zoomLevel}
crs={CRS.EPSG900913} // This works
crs={CONFIG.leafletMapCRS} // This doesn't
>
这是我的 config.json 文件
{
"leafletMapURL": "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
"leafletMapCRS": "CRS.EPSG900913"
}
LeafletMapURL 配置条目工作正常,但 LeafletMapCRS 由于某种原因不起作用。任何帮助表示赞赏。
解决方案
CRS
是具有某些属性的对象。在你的config.json
你正在把它变成一个字符串。因此,您需要做的是删除""
然后从'leaflet'
. 完成此操作后,您将看到您必须将文件设置为 a.js
而不是.json
能够导入CRS
.
因此,您将拥有:
import { CRS } from "leaflet";
export const config = () => {
return {
leafletMapURL: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
leafletMapCRS: CRS.EPSG900913
};
};
接着:
import { config } from "./config";
...
function App() {
const bounds = [[-26.5, -25], [1021.5, 1023]];
const configs = config();
return (
<Map
center={[0, 0]}
zoom={0}
style={{ height: "100vh" }}
crs={configs.leafletMapCRS} // This works
>
<ImageOverlay
url="https://leafletjs.com/examples/crs-simple/uqm_map_full.png"
bounds={bounds}
/>
</Map>
);
}
最后但并非最不重要的是,您确定可以使用CRS.EPSG900913
吗?因为这里描述了可用的参考系统,CRS.EPSG900913
但不包括在内。
推荐阅读
- javascript - 如何通过 ajax 将 JavaScript 变量转换为 PHP
- javascript - 布尔检查在 NodeJS 中不起作用
- c++11 - 如何在另一个命名空间中使用一个命名空间的指针,并且两个命名空间都在另一个类中使用
- mule - 如何在 Mule4 中生成动态 XML 响应?
- swift - 通过触摸该单元格中的按钮来删除 UITableViewCell
- javascript - 如何将 series-label 默认设置为 false 并更改 highchart 中系列标签文本的颜色
- c# - 用户双击时自动解密加密的exe文件的C#代码
- java - 无法通过 JAVA -8、64 位及更高版本的 NSIS 安装程序检查 Win-10 中已安装的 Java 版本
- testing - 如何使用马拉松测试工具(MarathonITE)从命令行运行几个测试用例或测试套件?
- vb.net - C1pdfviewer 页面大小问题