(地图瓦片纠偏最好的方法在这:https://www.cnblogs.com/s0611163/p/15606460.html)

地图区域是一个市,偏移量可以近似认为是固定不变的,通过修改Leaflet-src.js源码中的_update方法和_addTile方法对瓦片进行偏移纠偏。

Leaflet版本v1.3.4,要修改的_update和_addTile方法和最新版本1.6.0区别不大。

1、在_update方法中添加如下代码,瓦片图偏移后,在边缘位置需要补充瓦片图显示,不然边缘会出现空白:

        //处理纠偏后瓦片显示
var ratio = 1 / Math.pow(2, (18 - this._tileZoom)); //计算纠偏比率
var deltaX = 0;
var deltaY = 0;
if (this._map.options.offsetX) deltaX = this._map.options.offsetX * ratio / 256;
if (this._map.options.offsetY) deltaY = this._map.options.offsetY * ratio / 256;
if (deltaX > 0) tileRange.max.x += (Math.round(deltaX) + 1);
if (deltaY > 0) tileRange.max.y += (Math.round(deltaY) + 1);
if (deltaX < 0) tileRange.min.x -= (Math.floor(deltaX) - 1);
if (deltaY < 0) tileRange.min.y -= (Math.floor(deltaY) - 1);

2、在_update方法中修改如下代码:

            for (i = 0; i < queue.length; i++) {
this._addTile(queue[i], fragment, ratio);
}

3、在_addTile方法中添加如下代码,重新计算瓦片的像素位置:

        //纠偏
if (this._map.options.offsetX) tilePos.x -= Math.floor(this._map.options.offsetX * ratio);
if (this._map.options.offsetY) tilePos.y -= Math.floor(this._map.options.offsetY * ratio);

_update方法完整代码:

    // Private method to load tiles in the grid's active zoom level according to map bounds
_update: function (center) {
var map = this._map;
if (!map) { return; }
var zoom = this._clampZoom(map.getZoom()); if (center === undefined) { center = map.getCenter(); }
if (this._tileZoom === undefined) { return; } // if out of minzoom/maxzoom var pixelBounds = this._getTiledPixelBounds(center),
tileRange = this._pxBoundsToTileRange(pixelBounds),
tileCenter = tileRange.getCenter(),
queue = [],
margin = this.options.keepBuffer,
noPruneRange = new Bounds(tileRange.getBottomLeft().subtract([margin, -margin]),
tileRange.getTopRight().add([margin, -margin])); // Sanity check: panic if the tile range contains Infinity somewhere.
if (!(isFinite(tileRange.min.x) &&
isFinite(tileRange.min.y) &&
isFinite(tileRange.max.x) &&
isFinite(tileRange.max.y))) { throw new Error('Attempted to load an infinite number of tiles'); } for (var key in this._tiles) {
var c = this._tiles[key].coords;
if (c.z !== this._tileZoom || !noPruneRange.contains(new Point(c.x, c.y))) {
this._tiles[key].current = false;
}
} // _update just loads more tiles. If the tile zoom level differs too much
// from the map's, let _setView reset levels and prune old tiles.
if (Math.abs(zoom - this._tileZoom) > 1) { this._setView(center, zoom); return; } //处理纠偏后瓦片显示
var ratio = 1 / Math.pow(2, (18 - this._tileZoom)); //计算纠偏比率
var deltaX = 0;
var deltaY = 0;
if (this._map.options.offsetX) deltaX = this._map.options.offsetX * ratio / 256;
if (this._map.options.offsetY) deltaY = this._map.options.offsetY * ratio / 256;
if (deltaX > 0) tileRange.max.x += (Math.round(deltaX) + 1);
if (deltaY > 0) tileRange.max.y += (Math.round(deltaY) + 1);
if (deltaX < 0) tileRange.min.x -= (Math.floor(deltaX) - 1);
if (deltaY < 0) tileRange.min.y -= (Math.floor(deltaY) - 1); // create a queue of coordinates to load tiles from
for (var j = tileRange.min.y; j <= tileRange.max.y; j++) {
for (var i = tileRange.min.x; i <= tileRange.max.x; i++) {
var coords = new Point(i, j);
coords.z = this._tileZoom; if (!this._isValidTile(coords)) { continue; } var tile = this._tiles[this._tileCoordsToKey(coords)];
if (tile) {
tile.current = true;
} else {
queue.push(coords);
}
}
} // sort tile queue to load tiles in order of their distance to center
queue.sort(function (a, b) {
return a.distanceTo(tileCenter) - b.distanceTo(tileCenter);
}); if (queue.length !== 0) {
// if it's the first batch of tiles to load
if (!this._loading) {
this._loading = true;
// @event loading: Event
// Fired when the grid layer starts loading tiles.
this.fire('loading');
} // create DOM fragment to append tiles in one batch
var fragment = document.createDocumentFragment(); for (i = 0; i < queue.length; i++) {
this._addTile(queue[i], fragment, ratio);
} this._level.el.appendChild(fragment);
}
},

_addTile方法完整代码:

    _addTile: function (coords, container, ratio) {
var tilePos = this._getTilePos(coords),
key = this._tileCoordsToKey(coords); var tile = this.createTile(this._wrapCoords(coords), bind(this._tileReady, this, coords)); this._initTile(tile); // if createTile is defined with a second argument ("done" callback),
// we know that tile is async and will be ready later; otherwise
if (this.createTile.length < 2) {
// mark tile as ready, but delay one frame for opacity animation to happen
requestAnimFrame(bind(this._tileReady, this, coords, null, tile));
} //纠偏
if (this._map.options.offsetX) tilePos.x -= Math.floor(this._map.options.offsetX * ratio);
if (this._map.options.offsetY) tilePos.y -= Math.floor(this._map.options.offsetY * ratio); setPosition(tile, tilePos); // save tile in cache
this._tiles[key] = {
el: tile,
coords: coords,
current: true
}; container.appendChild(tile);
// @event tileloadstart: TileEvent
// Fired when a tile is requested and starts loading.
this.fire('tileloadstart', {
tile: tile,
coords: coords
});
},

如何使用:

1、JS引用由leaflet.js修改为引用leaflet-src.js

2、创建地图见如下代码,注意offsetX和offsetY参数,不同的城市,参数值不同,参数值可以用太乐地图下载器软件中的纠偏工具计算:

var map = new L.Map('map', { center: centerLatLng, zoom: 12, minZoom: 8, maxZoom: 18, maxBounds: mapBounds, offsetX: 1020, offsetY: 517, layers: [tileLayer], attributionControl: false, doubleClickZoom: false, zoomControl: false });

还有另一种纠偏方法,可以通过处理瓦片图进行纠偏:https://www.cnblogs.com/s0611163/p/12034779.html

Leaflet 地图偏移 地图纠偏的更多相关文章

  1. android 3.0+百度地图api地图如何移动到指定的经纬度处

    由于百度地图api,2.0+和3.0+的改动比较大,api基本上被全换过了,有些同学可能2.0+的api使用的非常熟悉,但是更新到3.0+时,却会遇到一些小麻烦(由于api变了,你就需要重新学习它的a ...

  2. JS 百度地图 换地图主题颜色(自定义)

    JS 百度地图 换地图主题颜色(自定义) 可通过这个在线编辑得到自己想要的主题:https://developer.baidu.com/map/custom/ <div id="all ...

  3. JS 百度地图 换地图主题颜色(API自带)

    JS 百度地图 换地图主题颜色(API自带) <script type="text/javascript" src="http://api.map.baidu.co ...

  4. leaflet创建简单地图

    一.leaflet介绍: 1.Leaflet 是一个为建设移动设备友好的互动地图,而开发的现代的.开源的 JavaScript 库.它是由 Vladimir Agafonkin 带领一个专业贡献者团队 ...

  5. 基于MySQL + Node.js + Leaflet的离线地图展示,支持百度、谷歌、高德、腾讯地图

    1. 基本说明 本项目实现了离线展示百度.谷歌.高德.腾讯地图.主要功能如下: 实现了地图瓦片图下载.存储.目前支持存储至MySQL Node.js服务调用MySQL中的瓦片图 Leaflet展示地图 ...

  6. 百度地图坐标偏移,微信小程序地图偏移问题,腾讯地图坐标偏移

    解决方案: 如果用百度的地图获取的坐标点,在微信小程序内使用,就会出现偏移 算法(lat和lng是经纬度,球面坐标): To_B是转到百度,To_G是转到GCJ-02(谷歌,高德,腾讯) var TO ...

  7. 小记 百度地图 soso地图 经纬度偏移

    项目里遇到了这么个问题,数据库原有数据是微信上用的,所以是soso地图坐标, 但是现在要做百度地图,坐标偏移严重,网上找了也没说偏移多少,自己手动测试10多分钟,得到个大概值,反正差不多就行了. so ...

  8. 百度地图移动版API 1.2.2版本(Android)地图偏移的最佳解决办法

    Import import com.baidu.mapapi.CoordinateConvert;import com.baidu.mapapi.GeoPoint; Code GeoPoint p = ...

  9. 如何使用android百度地图离线地图

    1.首先把离线地图放在android工程下的assets里面. 注意:建议离线地图下载通过百度地图APIDEMO去下载,因为到官网上下载的离线地图文件格式不一样,APIDEMO的格式是.dat,而官网 ...

  10. [OC][地图] 高德地图之定位初探(一)

    使用前的说明 高德地图开放平台的iOS定位模块网址-->http://lbs.amap.com/api/ios-location-sdk/summary/ 高德地图有Web端.android平台 ...

随机推荐

  1. Qt中QTabWidget添加控件(按钮,label等)以及使用方法

    今天遇到了一个问题,已经在QTabWidget每一行添加了一个按钮,我有一个需求就是,点击每一行的按钮都有各自的响应 首先说一下添加控件代码: 添加文字可以用setItem,添加控件就得用setCel ...

  2. java 405_Http状态405-方法不允许

    解决方法: 删除下列代码. super.doGet(req.resp); super.doPost(req.resp); 分析: 405错误一般指请求method not allowed 错误. 请求 ...

  3. java String字符串总结

    这里我们将总结字符串相关的知识,除了总结String的API用法,同时我们还会总结一些相关的知识点,包括字符串常量池.StringBuffer.StringBuilder,以及equals和==的用法 ...

  4. 【开源项目推荐】-支持GPT的智能数据库客户端与报表工具——Chat2DB

    2023年是人工智能爆火的一年,ChatGPT为首的一系列的大模型的出现,让生成式人工智能彻底火了一把.但有人会说,GPT对于我们数据开发来说并没有什么作用啊? 今天为大家推荐的开源项目,就是GPT在 ...

  5. RDBMS与Hbase对比 HDFS与HBase对比 Hive与HBase对比

    RDBMS: HBASE: HDFS与HBase对比: Hive与HBase对比: Hive与HBase总结

  6. 手把手教你用python做一个年会抽奖系统

    引言 马上就要举行年会抽奖了,我们都不知道是否有人能够中奖.我觉得无聊的时候可以尝试自己写一个抽奖系统,主要是为了娱乐.现在人工智能这么方便,写一个简单的代码不是一件困难的事情.今天我想和大家一起构建 ...

  7. 华企盾DSC导致wps个人模式无策略组新建的文件仍然加密

    解决方法:右键wps安装目录手动解密即可(原因:wps模板被加密导致)

  8. Scipy快速入门

    Scipy快速入门 注意事项 图床在国外,配合美区.日区网络使用更佳,如遇图片加载不出来,考虑换个VPN吧. 监修中敬告 本文处于Preview阶段,不对文章内容负任何责任,如有意见探讨欢迎留言. 联 ...

  9. OpenWRT的TTYD终端显示已拒绝连接

    更改openwrt软路由后台管理地址后,发现TTYD终端无法连接,显示已拒绝连接,无法使用的解决方法. 解决方法: 1.使用puty工具连接软路由 2.编辑ttyd配置文件 root@OpenWrt: ...

  10. 智能对联模型太难完成?华为云ModelArts助你实现!手把手教学

    摘要:农历新年将至,听说华为云 AI 又将开启智能对对联迎接牛气冲天,让我们拭目以待!作为资深 Copy 攻城狮,想要自己实现一个对对联的模型,是不能可能完成的任务,因此我搜罗了不少前人的实践案例,今 ...