leaflet加载实时路况图层(高德、百度)

加载百度地图实时路况

加载百度地图需要使用到的插件如下:proj4、proj4leaflet、tileLayer.baidu。其中tileLayer.baidu来自大佬——火星科技 木遥。

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
    <title>地图</title>
    <style>
        html,
        body,
        #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" />
    <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>
	<!-- 加载百度地图使用插件 -->
  <script src="https://cdn.bootcss.com/proj4js/2.4.3/proj4.js"></script>
    <script src="https://cdn.bootcss.com/proj4leaflet/1.0.1/proj4leaflet.min.js"></script>
    <script src="./tileLayer.baidu.js"></script>
</head>
<body>
    <div id="map" class="map">
    </div>
    <script type="text/javascript">

            //注意将map的crs赋值 crs: L.CRS.Baidu 详情请阅读示例页面 
            var map = L.map('map', {
                crs: L.CRS.Baidu,
                minZoom: 3,
                maxZoom: 18,
                attributionControl: false,
                center: [31.834912, 117.220102],
                zoom: 12
            });
			
			//控制地图底图
            L.control.layers({
                "百度地图": L.tileLayer.baidu({ layer: 'vec' }).addTo(map),
                "百度卫星": L.tileLayer.baidu({ layer: 'img' }),
                "百度地图-大字体": L.tileLayer.baidu({ layer: 'vec', bigfont: true }),
                "百度卫星-大字体": L.tileLayer.baidu({ layer: 'img', bigfont: true }),
                "自定义样式-黑色地图": L.tileLayer.baidu({ layer: 'custom', customid: 'dark' }),
                "自定义样式-蓝色地图": L.tileLayer.baidu({ layer: 'custom', customid: 'midnight' }) //自定义样式地图,customid可选值:dark,midnight,grayscale,hardedge,light,redalert,googlelite,grassgreen,pink,darkgreen,bluish
            }, {
                "实时交通信息": L.tileLayer.baidu({ layer: 'time' })
            }, { position: "topright" }).addTo(map);
       </script>

</body>

tileLayer.baidu插件如下,如果不知道在哪找到话,直接复制粘贴保存为js文件即可。

/** 
 * 百度地图底图调用插件
 * @author 火星科技 木遥原创(qq:346819890) 
 */
//请引入 proj4.js 和 proj4leaflet.js
L.CRS.Baidu = new L.Proj.CRS('EPSG:900913', '+proj=merc +a=6378206 +b=6356584.314245179 +lat_ts=0.0 +lon_0=0.0 +x_0=0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs', {
    resolutions: function () {
        level = 19
        var res = [];
        res[0] = Math.pow(2, 18);
        for (var i = 1; i < level; i++) {
            res[i] = Math.pow(2, (18 - i))
        }
        return res;
    }(),
    origin: [0, 0],
    bounds: L.bounds([20037508.342789244, 0], [0, 20037508.342789244])
});

L.tileLayer.baidu = function (option) {
    option = option || {};

    var layer;
    var subdomains = '0123456789';
    switch (option.layer) {
        //单图层
        case "vec":
        default:
            //'http://online{s}.map.bdimg.com/tile/?qt=tile&x={x}&y={y}&z={z}&styles=pl&b=0&limit=60&scaler=1&udt=20170525'
            layer = L.tileLayer('http://online{s}.map.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles=' + (option.bigfont ? 'ph' : 'pl') + '&scaler=1&p=1', {
                name:option.name,subdomains: subdomains, tms: true
            });
            break;
        case "img_d": 
            layer = L.tileLayer('http://shangetu{s}.map.bdimg.com/it/u=x={x};y={y};z={z};v=009;type=sate&fm=46', {
                name: option.name, subdomains: subdomains, tms: true
            });
            break;
        case "img_z":
            layer = L.tileLayer('http://online{s}.map.bdimg.com/tile/?qt=tile&x={x}&y={y}&z={z}&styles=' + (option.bigfont ? 'sh' : 'sl') + '&v=020', {
                name: option.name, subdomains: subdomains, tms: true
            });
            break;

        case "custom"://Custom 各种自定义样式
            //可选值:dark,midnight,grayscale,hardedge,light,redalert,googlelite,grassgreen,pink,darkgreen,bluish
            option.customid = option.customid || 'midnight';
            layer = L.tileLayer('http://api{s}.map.bdimg.com/customimage/tile?&x={x}&y={y}&z={z}&scale=1&customid=' + option.customid, {
                name: option.name, subdomains: "012", tms: true
            });
            break;

        case "time"://实时路况
            var time = new Date().getTime();
            layer = L.tileLayer('http://its.map.baidu.com:8002/traffic/TrafficTileService?x={x}&y={y}&level={z}&time=' + time + '&label=web2D&v=017', {
                name: option.name, subdomains: subdomains, tms: true
            });
            break;

            //合并
        case "img":
            layer = L.layerGroup([
				L.tileLayer.baidu({ name: "底图", layer: 'img_d', bigfont: option.bigfont }),
				L.tileLayer.baidu({ name: "注记", layer: 'img_z', bigfont: option.bigfont })
            ]);
            break;
    }
    return layer;
};

加载高德地图实时路况

加载高德地图使用“leaflet.ChineseTmsProviders”插件,需要使用Script引入js文件

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
    <title>地图</title>
    <style>
        html,
        body,
        #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" />
    <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>

	<!-- 高德地图使用插件 -->
	<script src="./leaflet.ChineseTmsProviders.js"></script>
</head>
<body>
    <div id="map" class="map">
    </div>
    <script type="text/javascript">
				//获取当前时间
				var time = new Date().getDate();
				var Gaode=new L.tileLayer.chinaProvider('GaoDe.Normal.Map', {
			        maxZoom: 18,
			        minZoom: 5
			    });
			    var map = L.map("map", {
			        center: [38,114.51],
			        zoom: 12,
			        layers: [Gaode]
			    });
			    L.control.layers(null,{
					"高德路况":L.tileLayer('http://tm.amap.com/trafficengine/mapabc/traffictile?v=1.0&;t=1&x={x}&y={y}&z={z}&&t='+time)
				}).addTo(map);

    </script>

</body>

后记:在leaflet中挺好用的一个小功能L.control.layers(option1,option2)。功能为,为地图添加小控件,可以用来进行多图层的切换或者添加图层,其中option1中的均为单选按钮,option2中则为多选按钮,需要按情况选择放在哪里,如果不需要的话直接赋值null即可。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现leaflet加载自定义高德地图后坐标类型为gcj02,你需要进行以下步骤: 1. 在高德开放平台上申请自定义地图服务并获取到对应的key。 2. 在Leaflet中添加高德地图服务的图层,示例代码如下: ``` L.tileLayer('https://webst01.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}', { attribution: '高德地图', maxZoom: 18 }).addTo(map); ``` 3. 在Leaflet中添加坐标转换插件,这里推荐使用proj4leaflet插件。在使用之前,需要将proj4库引入到项目中。示例代码如下: ``` // 添加proj4库 <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.6.2/proj4.js"></script> // 定义高德地图的投影坐标系 proj4.defs("EPSG:102100", "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"); // 在Leaflet中添加坐标转换插件 L.Proj.geoJson(data, { 'pointToLayer': function(feature, latlng) { return L.marker(latlng); } }, { 'coordsToLatLng': function(coords) { var point = L.point(coords[0], coords[1]); point = L.Proj.transform(proj4.defs("EPSG:102100"), L.CRS.GCJ02.projection, point); return L.CRS.GCJ02.pointToLatLng(point); } }).addTo(map); ``` 4. 将Leaflet的坐标系设置为GCJ02,示例代码如下: ``` L.CRS.GCJ02 = L.extend({}, L.CRS.EPSG3857, { // 将EPSG3857的投影坐标系转换为GCJ02的投影坐标系 transformation: L.transformation(1 / 128, -1 / 128, -1 / 128, 1 / 128), // 将EPSG3857的地理坐标系转换为GCJ02的地理坐标系 code: 'GCJ02' }); // 将地图的坐标系设置为GCJ02 map.options.crs = L.CRS.GCJ02; ``` 通过以上步骤,你就可以实现leaflet加载自定义高德地图后坐标类型为gcj02了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值