百度舆图依据已知坐标画轨迹

原文来自 taoeer.top

百度舆图依据已知坐标画点线,这两天用到的,在这里做个笔记,轻易本身查阅!

var map = new BMap.Map("map");
    var point = new BMap.Point(116.404, 39.915);
    map.centerAndZoom(point, 15);
    map.enableScrollWheelZoom(); // 开启鼠标滚轮缩放
    map.addControl(new BMap.ScaleControl()); // 增添比例尺控件

    var pointStr = "104.038748,30.641821,104.047789,30.648128,104.063959,30.655336,104.067264,30.660307,104.067264,30.664438,104.064008,30.665316".split(",");
    var pointArr = [];
    
    for (var k = 0; k < pointStr.length; k += 2) {
        pointArr.push({
            lng: pointStr[k],
            lat: pointStr[k + 1]
        });
    }
    
    // 天生坐标点
    var trackPoint = [];
    for (var i = 0, j = pointArr.length; i < j; i++) {
        trackPoint.push(new BMap.Point(pointArr[i].lng, pointArr[i].lat));
    }

    map.centerAndZoom(trackPoint[0], 15);
    
    // 画线
    var polyline = new BMap.Polyline(trackPoint, {
        strokeColor: "#1869AD",
        strokeWeight: 3,
        strokeOpacity: 1
    });
    map.addOverlay(polyline);
       
    // 设置图片
    var size = new BMap.Size(26, 26);
    var offset = new BMap.Size(0, -13);
    var imageSize = new BMap.Size(26, 26);
    var icon = new BMap.Icon("./express-position.png", size, {
        imageSize: imageSize
    });
    
    // 画图标
    for (var i = 0, j = trackPoint.length; i < j; i++) {
        var marker = new BMap.Marker(trackPoint[i], {
            icon: icon,
            offset: offset
        }); // 建立标注    
        map.addOverlay(marker);

    }

    //依据经纬极值盘算绽放级别。 (从网上复制)
    function getZoom(maxLng, minLng, maxLat, minLat) {
        var zoom = ["50", "100", "200", "500", "1000", "2000", "5000", "10000", "20000", "25000", "50000", "100000", "200000", "500000", "1000000", "2000000"]; // 级别18到3。
        var pointA = new BMap.Point(maxLng, maxLat); // 建立点坐标A
        var pointB = new BMap.Point(minLng, minLat); // 建立点坐标B
        var distance = map.getDistance(pointA, pointB).toFixed(1); //猎取两点间隔,保存小数点后两位
        for (var i = 0, zoomLen = zoom.length; i < zoomLen; i++) {
            if (zoom[i] - distance > 0) {
                return 18 - i + 3; //之所以会多3,是因为舆图局限常常是比例尺间隔的10倍以上。所以级别会增添3。
            }
        }
    }
    
    //  (从网上复制)
    function setZoom(points) {
        if (points.length > 0) {
            var maxLng = points[0].lng;
            var minLng = points[0].lng;
            var maxLat = points[0].lat;
            var minLat = points[0].lat;
            var res;
            for (var i = points.length - 1; i >= 0; i--) {
                res = points[i];
                if (res.lng > maxLng) maxLng = res.lng;
                if (res.lng < minLng) minLng = res.lng;
                if (res.lat > maxLat) maxLat = res.lat;
                if (res.lat < minLat) minLat = res.lat;
            }
            var cenLng = (parseFloat(maxLng) + parseFloat(minLng)) / 2;
            var cenLat = (parseFloat(maxLat) + parseFloat(minLat)) / 2;
            var zoom = getZoom(maxLng, minLng, maxLat, minLat);
            map.centerAndZoom(new BMap.Point(cenLng, cenLat), zoom);
        } else {
            //没有坐标,显现全中国
            map.centerAndZoom(new BMap.Point(103.388611, 35.563611), 5);
        }
    }

    setZoom(pointArr)
    原文作者:c1生不换
    原文地址: https://segmentfault.com/a/1190000007319657
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞