我正在尝试扩展'react-leaflet' v3中的TileLayer组件。需要覆盖此函数以提供自定义的瓦片URL命名方案。 以下是基本leaflet示例:
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 ^3.2.0
和leaflet ^1.7.1
。 - Gunner@react-leaflet/core ^1.0.2
。将版本升级到@react-leaflet/core ^1.1.0
后问题得到解决。感谢您的帮助。在设置Stackblitz链接时解决了这个问题。 - Gunner