使用Leaflet经纬网格插件
介绍:
本文档介绍了如何在使用Leaflet地图库时使用经纬网格插件,以便在地图上显示经纬度网格线。该插件的地址为
步骤:
以下是在使用Leaflet地图库时使用经纬网格插件的步骤:
- 下载插件:
在浏览器中打开插件的GitHub地址https://github.com/812781385/leaflet.latlng-graticule.git,并下载插件的源代码。
- 引入插件:
将下载的插件源代码解压缩,并将解压后的
latlng-graticule.js
文件复制到您的项目目录中。
- 在使用处引入插件:
import “@/plugin/leaflet.latlng-graticule.js”;
- 使用插件:
// 经纬网格
export const useDrawGraticule = (item: any) => {
// obj 你的全局对象
if (item.checked) {
obj[item.id] = L.latlngGraticule({
showLabel: true,
color: "#000000",
dashArray: [5, 5],
zoomInterval: [
{start: 2, end: 3, interval: 30},
{start: 4, end: 4, interval: 10},
{start: 5, end: 7, interval: 5},
{start: 8, end: 10, interval: 1}
]
}).addTo(mapView); // mapView你的地图元素
return;
}
obj[item.id] && obj[item.id].remove();
};
- 修改插件源码
修改文件import “@/plugin/leaflet.latlng-graticule.js”;里的代码。
删除// todo: format type of float
下面的代码,改成return this.calculate(lng)
calculate: function (lng) {
if (lng == 180 || lng == 0) {
return lng + this.options.sides[3];
}
else if (lng > 180) {
if (360 - Math.abs(lng) >= 0) {
const newLng = 360 - lng;
return newLng + this.options.sides[3];
}
const newLng = Math.abs(360 - lng);
return this.calculate(newLng)
}
else if (lng < 0) {
if (180 + lng >= 0) {
return Math.abs(lng) + this.options.sides[3];
}
if (360 + lng >= 0) {
return 360 + lng + this.options.sides[2];
}
return this.calculate(360 + lng)
}
else {
if (lng > 0) {
return lng + this.options.sides[2];
}
const newLng = Math.abs(lng);
return lng
}
},
__format_lng: function (lng) {
if (this.options.lngFormatTickLabel) {
return this.options.lngFormatTickLabel(lng);
}
return this.calculate(lng)
// todo: format type of float
// if (lng > 180) {
// return '' + (360 - lng) + this.options.sides[3];
// }
// else if (lng > 0 && lng < 180) {
// return '' + lng + this.options.sides[2];
// }
// else if (lng < 0 && lng > -180) {
// return '' + (lng * -1) + this.options.sides[3];
// }
// else if (lng == -180) {
// return '' + (lng * -1);
// }
// else if (lng < -180) {
// return '' + (360 + lng) + this.options.sides[3];
// }
// return '' + lng;
},
总结:
本文介绍了如何在使用Leaflet地图库时使用经纬网格插件。通过按照以上步骤引入和配置插件,您可以在地图上显示经纬度网格线。
希望这份文章对您有所帮助!