FarmOS 地图模块 上手教程
关联项目:FarmOS 地图模块 前置要求:Node.js 与 npm;了解 JavaScript 模块、OpenLayers 基础与 GeoJSON 格式;了解 farmOS 资产/日志概念(独立使用可略)。 预计耗时:本地构建约 15–30 分钟;跑通示例地图约 30 分钟;与 farmOS 集成约 1–2 小时。 官方仓库:github.com/farmOS/farmOS-map 前置要求:Node.js 与 npm;了解 JavaScript 模块、OpenLayers 基础与 GeoJSON 格式;了解 farmOS 资产/日志概念(独立使用可略)。
预计耗时:本地构建约 15–30 分钟;跑通示例地图约 30 分钟;与 farmOS 集成约 1–2 小时。
官方仓库:github.com/farmOS/farmOS-map
本教程带你完成从环境准备到跑通核心工作流的完整过程。命令与示例针对主流环境给出,实际版本参数请以官方文档为准。
一、环境准备
克隆仓库并安装依赖:
git clone https://github.com/farmOS/farmOS-map.git
cd farmOS-map
node --version
npm --version
npm install
Node 版本以仓库 package.json 的 engines 字段为准。
二、安装部署
构建并检查产物:
npm run build
ls -l dist
预期输出:构建成功,dist 目录出现 JS/CSS 产物。若需跑测试或启动示例,脚本名以仓库 package.json 为准(常见为 npm test、npm start)。
三、快速上手:跑通第一个示例
用 OpenLayers 起一张带 OSM 底图和 GeoJSON 图层的地图(farmOS-map 的封装 API 以官方文档为准):
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
import OSM from 'ol/source/OSM.js';
import VectorLayer from 'ol/layer/Vector.js';
import VectorSource from 'ol/source/Vector.js';
import GeoJSON from 'ol/format/GeoJSON.js';
const source = new VectorSource({
url: 'fields.geojson',
format: new GeoJSON(),
});
const map = new Map({
target: 'map',
layers: [
new TileLayer({ source: new OSM() }),
new VectorLayer({ source }),
],
view: new View({ center: [0, 0], zoom: 2 }),
});
target 对应页面中 id="map" 的元素;VectorSource 从 fields.geojson 加载地块边界;View 设置初始中心与缩放。center 使用 EPSG:3857 坐标,经纬度需先投影转换。
四、核心工作流
任务:在地图上显示一组地块并允许编辑边界
- 准备 GeoJSON(可来自 farmOS API 的几何字段):
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {"name": "Plot A"},
"geometry": {
"type": "Polygon",
"coordinates": [[[0,0],[0,0.01],[0.01,0.01],[0.01,0],[0,0]]]
}
}]
}
-
初始化地图与矢量图层(见 t_quickstart 示例),确认浏览器显示多边形。
-
加入编辑交互:
import Draw from 'ol/interaction/Draw.js';
import Modify from 'ol/interaction/Modify.js';
const draw = new Draw({ source, type: 'Polygon' });
const modify = new Modify({ source });
map.addInteraction(draw);
map.addInteraction(modify);
预期:可在地图上绘制新多边形并拖动顶点修改。
- 导出编辑结果:
const format = new GeoJSON();
const geojson = format.writeFeatures(source.getFeatures());
console.log(geojson);
- 保存到后端:把
geojsonPOST 到 farmOS 相应字段端点(接口以官方文档为准)。
五、进阶实践
- 底图切换:替换
TileLayer的 source 为 XYZ 或 WMS,影像底图需配置相应 URL 与授权。 - 坐标转换:
import { fromLonLat } from 'ol/proj.js'; view.setCenter(fromLonLat([116.4, 39.9]))。 - 要素选择:
new Select({ condition: click })实现点击高亮并读取properties.name。 - 样式定制:
new Style({ stroke: ..., fill: ... })控制地块颜色与边框。 - 与 farmOS 集成:使用 JSON:API 获取/提交几何字段,认证方式以官方文档为准。
六、常见问题
Q:地图上要素位置不对?
A:检查坐标系:GeoJSON 默认 EPSG:4326(经纬度),而 View 默认 EPSG:3857,加载时指定正确的 dataProjection。
Q:npm run build 失败?
A:确认 Node 版本符合要求、依赖已安装;查看报错是否来自 peer dependency 冲突。
Q:farmOS-map 能单独用吗? A:可以,它是独立 JS 库;但部分 API 面向 farmOS 数据结构,独立使用时需自行适配。
Q:支持离线地图吗? A:需自行准备离线瓦片或本地 GeoJSON;默认 OSM 底图需要网络。
七、学习资源
- 仓库与 README:https://github.com/farmOS/farmOS-map
- OpenLayers 教程:https://openlayers.org/en/latest/doc/tutorials/
- GeoJSON 规范:https://geojson.org/
- farmOS 文档:https://farmos.org/guide/