Leaflet使用Nginx代理,实现内网访问外网百度地图瓦片

5 篇文章 3 订阅
4 篇文章 0 订阅

解决思路:
1.在瓦片地址前加上内网ip和端口
2.在可同时访问内网和外网的机器上部署nginx
3.通过nginx监听内网端口,把瓦片的请求转发到外网

地图页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Leaflet使用Nginx代理实现内网访问百度地图瓦片</title>
<link rel="stylesheet" href="js/leaflet/leaflet.css" />
<script src="js/leaflet/leaflet.js"></script>
<script src="js/leaflet/proj4-compressed.js"></script>
<script src="js/leaflet/proj4leaflet.js"></script>
<script src="js/leaflet/leaflet-bmap.js"></script>

<style>
	html,
    body { 
    	height: 100%;
		margin: 0;
	}
	.lmap {
		width: 100%; 
		height: 100%;	
	}
</style>
</head>
    <body>
        <div id="lmap" class="lmap"></div>
        
        <script>
			const lmap = L.map('lmap', { crs: crs, center: L.latLng(34.35323, 108.941291), zoom: 4 });

            new L.TileLayer('http://192.168.1.111:9998/online{s}.map.bdimg.com/tile/?qt=tile&x={x}&y={y}&z={z}&styles=pl', {
                maxZoom: 21,
                minZoom: 3,
                subdomains: [0, 1, 2],
                tms: true,
            }).addTo(lmap);
            
        </script>
    </body>
</html>
Nginx代理配置
server {
    listen       9998;
    server_name  192.168.1.111;
    
    location /online0.map.bdimg.com/ {
        proxy_pass http://online0.map.bdimg.com/;
    }

    location /online1.map.bdimg.com/ {
        proxy_pass http://online1.map.bdimg.com/;
    }

    location /online2.map.bdimg.com/ {
        proxy_pass http://online2.map.bdimg.com/;
    }

    location /online3.map.bdimg.com/ {
        proxy_pass http://online3.map.bdimg.com/;
    }

    location /online4.map.bdimg.com/ {
        proxy_pass http://online4.map.bdimg.com/;
    }
}

页面效果

在这里插入图片描述

参考资料:https://blog.csdn.net/qq_38491310/article/details/87301919

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值