BIGEMPA Js API示例中心

动态贴图 修改尺寸源代码展示

代码编辑区 运行 下载 还原
<!DOCTYPE html>

<html>
<head>
    <meta charset='UTF-8' />
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no'/>
    <link href='http://bigemap.com:9000/bigemap.js/v2.1.0/bigemap.css' rel='stylesheet'/>
    <link href="http://www.bigemap.com/Public/css/button.min.css" rel="stylesheet">

    <script src='http://bigemap.com:9000/bigemap.js/v2.1.0/bigemap.js'></script>
    <style>
        body { margin: 0; padding: 0; }
        #map { position: absolute; top: 0; bottom: 0; width: 100%; }
        #form {
            position: absolute;
            top: 30px;
            right: 30px;
            text-align: right;
        }
        .my-div-icon {
            border-radius: 5px;
            background-color: #333;
            opacity: 0.9;
            box-shadow: 0px 0px 8px #000
        }
        #text {
            color: #f00;
        }
        
    </style>
    <title>动态贴图 修改尺寸</title>
</head>
<body>
    <div id='map'></div>
    <div id="form">
        <label for="upload" class="button button-tiny button-rounded button-primary">上传</label>
        <input type="file" id="upload" accept=".jpg,.jpeg,.png" hidden>
        <p id="text"></p>
    </div>

    <script>
        BM.Config.HTTP_URL = 'http://bigemap.com:9000';
        var map = BM.map('map', 'bigemap.amap-map', { center: [0, 0], zoom: 3, zoomControl: false });
        window.map = map                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        window.BM = BM
        
        var imageBounds = [ 
            [39.939229, 116.327911,],
            [39.946275, 116.342659,]
        ]
       
        var imagesOverlay = BM.imageOverlay('/offline_data/images/dongwuyuan.jpg',imageBounds, {
            bubblingMouseEvents: false, // 如果true,此图层上的鼠标事件将在地图上触发相同的事件(除非BM.DomEvent.stopPropagation使用)。
            interactive: true, // 如果true,图像叠加将在单击或悬停时发出鼠标事件。
            zIndex: 9,
            className: 'myImagesOverlay'
        }).addTo(map)

        var bounds = imagesOverlay.getBounds()
        map.fitBounds(bounds)
        var featureGroup = create(bounds, imagesOverlay)

        featureGroup.addTo(map)
        

        var upload = document.querySelector('#upload')
        var name = null
        upload.onchange = (e) => {
            var files = e.target
            if(name == files.files[0].name) {
                document.querySelector('#text').innerHTML = '两次图片不能相同'
                return
            }
            if (!checkFormat(files.files[0])) {
                document.querySelector('#text').innerHTML = '请选择jpg, png, jpeg格式'
                return;
            }
            document.querySelector('#text').innerHTML = ''
            var reader = new FileReader()
            reader.readAsDataURL(files.files[0]);
            reader.onload = function(e) {
                var data = e.target.result
                // 处理文件数据
                imagesOverlay.setUrl(data)
                name = files.files[0].name
                files.value = null
            }
        }

        function create(bounds, imagesOverlay) {
            var markerList = []
            for(var key in bounds) {
                if (bounds.hasOwnProperty(key)) {
                    var icon = BM.divIcon({
                        html: '<div></div>',
                        // iconAnchor: [30, 0],
                        iconSize: [16, 16],
                        className: 'my-div-icon'
                    });
                    var marker = BM.marker(bounds[key],{
                        icon: icon,
                        draggable:true
                    });
                    marker.text = key
                    markerList.push(marker)
                }
            }
            var featureGroup = BM.featureGroup([...markerList])//featureGroup会传播绑定在上面的事件

            featureGroup.eachLayer(function (layer) {
                layer.on('move',function (e) {
                    bounds[layer.text] = e.latlng
                    imagesOverlay.setBounds(bounds);
                })
            })
            return featureGroup
        }

        function checkFormat(o) {
            var filename = o.name;//文件名称
            var suffix = filename.substr(filename.lastIndexOf('.') + 1).toLowerCase();  //png jpg等
            var file_format = ['jpg', 'png', 'jpeg'];
            // 看看这个后缀存不存在于file_format数组当中
            if (file_format.includes(suffix)) {
                return true
            }
            return false
        }


    </script>
</body>
</html>