使用leaflet库加载服务器离线地图瓦片(这边以本地nginx服务器为例)

1,下载nginx并配置nginx配置文件(nginx.conf中配置跨域和本地服务图片地址)

javascript 复制代码
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       1995;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            root   D:/gmaps; #瓦片地址
            add_header 'Access-Control-Allow-Origin' '*';
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

2,leaflet中使用

html 复制代码
<!DOCTYPE html>
<html>
<head>
  <title>本地显示谷歌地图瓦片</title>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

  <link rel="stylesheet" href="leaflet.css" />
   
  <script src="leaflet.js"></script>

  <link rel="stylesheet" href="L.Control.Zoominfo.css" />
  <script src="L.Control.Zoominfo.js" ></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
  <script src="leaflet-tilelayer-colorizr.js"></script>
<script src="https://unpkg.com/proj4leaflet@1.4.2/lib/proj4js-compressed.js"></script>
<script src="https://unpkg.com/proj4leaflet@1.4.2/lib/proj4leaflet-compressed.js"></script>
<script src="https://unpkg.com/leaflet-google-layer/dist/leaflet-google-layer.js"></script>

  <style>
    body {
      padding: 0;
      margin: 0;
    }

    html, body, #map {
      height: 100%;
    }
#map {


  
}
#mapp{
  webkit-filter: invert(1) saturate(0.4) brightness(1.6)!important;
    -moz-filter: invert(1) saturate(0.4) brightness(1.6)!important;
    filter: invert(1) saturate(1) brightness(0.6)!important; 
     filter: invert(90%) hue-rotate(180deg);
  filter: brightness(0.6)contrast(1.2)grayscale(0.3);
              -webkit-filter: invert(50%) grayscale(0) saturate(0.5) brightness(1.6) opacity(1) hue-rotate(334deg) sepia(10%) !important;
            -ms-filter: invert(1) grayscale(0) saturate(0.5) brightness(1.6) opacity(1) hue-rotate(334deg) sepia(10%) !important;
            -moz-filter: invert(1) grayscale(0) saturate(0.5) brightness(1.6) opacity(1) hue-rotate(334deg) sepia(10%) !important;
            filter: invert(1) grayscale(0) saturate(0.5) brightness(1.6) opacity(1) hue-rotate(334deg) sepia(1%) !important;
}
.leaflet-zoom-animated img {

          } 

  </style>
</head>
<body>
  <div id="map"></div>
  
  <script>
    var map = L.map('map', {
      zoominfoControl: true,
      zoomControl: false,
      zoom: 5, 
      //center: [31.22, 121.48], 
      attributionControl: true 
    }).setView([23.3725, 116.4238], 5);
    //var map = L.map('map').setView([23.1291, 113.2644], 13);
   /*L.tileLayer('gmaps/{z}/{x}/{y}.png', {

      maxZoom: 15,
 
      attribution: 'Map data &copy; GoogleMap,Support by <a href="http://rovertang.com/">RoverTang</a>'
    }).addTo(map);*/

L.tileLayer.colorizr('http://192.168.2.25:1995/{z}/{x}/{y}.png', {

      maxZoom: 6,
      crossOrigin: "Anonymous",
      attribution: 'Map data &copy; GoogleMap,Support by <a href="http://rovertang.com/">RoverTang</a>',
      colorize: function (pixel) {
            // 这个方法用来调整所有的图片上的rgb值,pixel是图片原有的rgb值
           pixel.r += 24;
           pixel.g += 24;
           pixel.b += 24;
           // pixel.a+=0.1;
           return pixel;
       }
    }).addTo(map);

     var marker = L.marker([22.7457, 113.9454]).addTo(map)
    .bindPopup("这是一个标注点")
    .openPopup();
    var marker = L.marker([35.86, 99.66]).addTo(map)
    .bindPopup("这是二个标注点")
    .openPopup();
      var marker = L.marker([23.6673527777777, 104.385552777777]).addTo(map)
    .bindPopup("这是二个标注点")
    .openPopup();
 
  </script>
</body>
</html>
相关推荐
硬件工艺分享君2 分钟前
PCB厂家如何守住交期、品质与响应
大数据·运维·科技·制造·pcb工艺
库玛西4 分钟前
Linux网络编程:HTTP/HTTPS协议核心技术全解析
linux·运维·服务器·网络·c++·http·https
志栋智能17 分钟前
大模型驱动运维:重构故障根因分析的“思考”逻辑
运维·服务器·重构·自动化
_F_y1 小时前
Linux网络常用指令
linux·运维·网络
.Hypocritical.1 小时前
Tomcat本地部署+远程服务器部署超详细教程
java·服务器·tomcat
段一凡-华北理工大学1 小时前
高炉炉况智能诊断与预警实战~系列文章18:预警提前量问题:过程滞后与预测窗口的权衡
服务器·网络·人工智能·机器学习·高炉智能化·炉况预警·工业预警滞后
北风toto1 小时前
扩展操作码技术 · 详细笔记
运维·服务器·笔记·软件设计师
笑霸final1 小时前
不用上传服务器!用 Vue3 + ONNX Runtime Web 在浏览器本地实现智能抠图
运维·前端·图像处理·ai·onnx·canva可画·web性能优化
2601_962218472 小时前
万象生鲜系统跨仓调拨算法助力生鲜企业供应链数字化协同运营
大数据·运维·微服务·云原生·架构
Dovis(誓平步青云)2 小时前
拍视频前先把镜头想清楚:做一个分镜取景辅助器
android·java·服务器·javascript·人工智能