【发布时间】:2021-01-11 08:55:23
【问题描述】:
我正在尝试在“react-leaflet”v3 中扩展 TileLayer 组件。有必要重写此函数以提供自定义磁贴 URL 命名方案。 我需要的一个例子,写在基本的传单中:
function initMap() {
L.TileLayer.WebGis = L.TileLayer.extend({
initialize: function (url, options) {
options = L.setOptions(this, options);
if (options.detectRetina && L.Browser.retina && options.maxZoom > 0) {
options.tileSize = Math.floor(options.tileSize / 2);
options.zoomOffset++;
if (options.minZoom > 0) {
options.minZoom--;
}
this.options.maxZoom--;
}
if (options.bounds) {
options.bounds = L.latLngBounds(options.bounds);
}
this._url = url + "/gis_render/{x}_{y}_{z}/" + options.userId + "/tile.png";
var subdomains = this.options.subdomains;
if (typeof subdomains === 'string') {
this.options.subdomains = subdomains.split('');
}
},
getTileUrl: function (tilePoint) {
return L.Util.template(this._url, L.extend({
s: this._getSubdomain(tilePoint),
z: 17 - this._map._zoom,
x: tilePoint.x,
y: tilePoint.y
}, this.options));
}
});
L.tileLayer.webGis = function (url, options) {
return new L.TileLayer.WebGis(url, options);
};
// create a map in the "map" div, set the view to a given place and zoom
var map = L.map('map').setView([53.9, 27.55], 10);
// add an Gurtam Maps tile layer
L.tileLayer.webGis(wialon.core.Session.getInstance().getBaseGisUrl('render'), {
attribution: 'Gurtam Maps',
minZoom: 4,
userId: wialon.core.Session.getInstance().getCurrUser().getId()
}).addTo(map);
}
如果我只是将 Gurtam 地图的 url 写入 TileLayer 组件的 'url' 属性,那么我的地图显示不正确(缩放和平铺错误)。
我不知道用什么来正确显示:
- 使用“useRef”挂钩获取当前 TileLayer 实例并对其进行扩展。
- 使用包“react-leaflet/core”中的一些钩子(可能是 createElementHook)并创建我自己的自定义组件
- 或者别的什么
如有任何解释,我将不胜感激。
【问题讨论】:
标签: react-leaflet