解决百度UEditor富文本地图控件不支持https的问题

第一:

在map.html和show.html的head标签中添加:

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

第二:

修改1.1到2.0升级的部分api:
map.html:
改前:

Line 8 <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.1&services=true"></script>

改后:

Line 8 <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=百度地图秘钥&s=1"></script>

改前:

Line 57  marker.setPoint(point);

改后:

Line 57  marker.setPosition(point);

改前:

Line 109  var zoom = map.zoomLevel();

改后:

Line 109  var zoom = map.getZoom();

改前:

Line 113  var point = marker.getPoint();

改后:

Line 113  var point = marker.getPosition();

改前:

Line 126  var url = "http://api.map.baidu.com/staticimage?center=" + center.lng + ',' + center.lat +

改后:

Line 126  var url = "https://api.map.baidu.com/staticimage?s=1&center=" + center.lng + ',' + center.lat +

show.html:
改前:

Line 17  <script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=1.1&services=true"></script>

改后:

Line 17  <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=百度地图秘钥&s=1"></script>

改前:

Line 109  marker = window.marker.getPoint();

改后:

Line 109  marker = window.marker.getPosition();

ok!


参考网址:

  1. https://blog.csdn.net/qq_41408081/article/details/89958598
  2. https://blog.csdn.net/stacy06/article/details/82626363

map.html 文件修改后的

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
    <title></title>
    <script type="text/javascript" src="../internal.js"></script>
    <!--<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.1&services=true"></script> -->
    <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=百度地图秘钥&s=1"></script>
    <style type="text/css">
        .content{width:530px; height: 350px;margin: 10px auto;}
        .content table{width: 100%}
        .content table td{vertical-align: middle;}
        #city,#address{height:21px;background: #FFF;border:1px solid #d7d7d7; line-height: 21px;}
        #city{width:60px}
        #address{width:130px}
        #is_dynamic_label span{vertical-align:middle;margin: 3px 0px 3px 3px;}
        #is_dynamic_label input{vertical-align:middle;margin: 3px 3px 3px 50px;}
    </style>
</head>
<body>
<div class="content">
    <table>
        <tr>
            <td><var id="lang_city"></var>:</td>
            <td><input id="city" type="text" /></td>
            <td><var id="lang_address"></var>:</td>
            <td><input id="address" type="text" value="" /></td>
            <td><a href="javascript:doSearch()" class="button"><var id="lang_search"></var></a></td>
            <td><label id="is_dynamic_label" for="is_dynamic"><input id="is_dynamic" type="checkbox" name="is_dynamic" /><span><var id="lang_dynamicmap"></var></span></label></td>
        </tr>
    </table>
    <div style="width:100%;height:340px;margin:5px auto;border:1px solid gray" id="container"></div>

</div>
<script type="text/javascript">
    var map = new BMap.Map("container"),marker,point,styleStr;
    map.enableScrollWheelZoom();
    map.enableContinuousZoom();
    function doSearch(){
        if (!document.getElementById('city').value) {
            alert(lang.cityMsg);
            return;
        }
        var search = new BMap.LocalSearch(document.getElementById('city').value, {
            onSearchComplete: function (results){
                if (results && results.getNumPois()) {
                    var points = [];
                    for (var i=0; i<results.getCurrentNumPois(); i++) {
                        points.push(results.getPoi(i).point);
                    }
                    if (points.length > 1) {
                        map.setViewport(points);
                    } else {
                        map.centerAndZoom(points[0], 13);
                    }
                    point = map.getCenter();
                    //marker.setPoint(point);
                    marker.setPosition(point);
                } else {
                    alert(lang.errorMsg);
                }
            }
        });
        search.search(document.getElementById('address').value || document.getElementById('city').value);
    }
    //获得参数
    function getPars(str,par){
        var reg = new RegExp(par+"=((\\d+|[.,])*)","g");
        return reg.exec(str)[1];
    }
    function init(){
        var mapNode = editor.selection.getRange().getClosedNode(),
            isMapImg = mapNode && /api[.]map[.]baidu[.]com/ig.test(mapNode.getAttribute("src")),
            isMapIframe = mapNode && domUtils.hasClass(mapNode, 'ueditor_baidumap');
        if(isMapImg || isMapIframe){
            var url, centerPos, markerPos;
            if(isMapIframe) {
                url = decodeURIComponent(mapNode.getAttribute("src"));
                $G('is_dynamic').checked = true;
                styleStr = mapNode.style.cssText;
            } else {
                url = mapNode.getAttribute("src");
                styleStr = mapNode.style.cssText;
            }

            centerPos = getPars(url,"center").split(",");
            markerPos = getPars(url, "markers").split(",");
            point = new BMap.Point(Number(centerPos[0]),Number(centerPos[1]));
            marker = new BMap.Marker(new BMap.Point(Number(markerPos[0]), Number(markerPos[1])));
            map.addControl(new BMap.NavigationControl());
            map.centerAndZoom(point, Number(getPars(url,"zoom")));
        }else{
            point = new BMap.Point(116.404, 39.915);    // 创建点坐标
            marker = new BMap.Marker(point);
            map.addControl(new BMap.NavigationControl());
            map.centerAndZoom(point, 10);                     // 初始化地图,设置中心点坐标和地图级别。
        }
        marker.enableDragging();
        map.addOverlay(marker);
    }
    init();
    document.getElementById('address').onkeydown = function (evt){
        evt = evt || event;
        if (evt.keyCode == 13) {
            doSearch();
        }
    };
    dialog.onok = function (){
        var center = map.getCenter();
        var zoom = map.getZoom();
       // var zoom = map.zoomLevel;
        var size = map.getSize();
        var mapWidth = size.width;
        var mapHeight = size.height;
        //var point = marker.getPoint();
        var point = marker.getPosition();

        if($G('is_dynamic').checked) {
            var URL = editor.options.UEDITOR_HOME_URL,
                url = [URL + (/\/$/.test(URL) ? '':'/') + "dialogs/map/show.html" +
                    '#center=' + center.lng + ',' + center.lat,
                    '&zoom=' + zoom,
                    '&width=' + mapWidth,
                    '&height=' + mapHeight,
                    '&markers=' + point.lng + ',' + point.lat,
                    '&markerStyles=' + 'l,A'].join('');
            editor.execCommand('inserthtml', '<iframe class="ueditor_baidumap" src="' + url + '"' + (styleStr ? ' style="' + styleStr + '"' :'') + ' frameborder="0" width="' + (mapWidth+4) + '" height="' + (mapHeight+4) + '"></iframe>');
        } else {
           // var url = "http://api.map.baidu.com/staticimage?center=" + center.lng + ',' + center.lat +
              var url = "https://api.map.baidu.com/staticimage?s=1&center=" + center.lng + ',' + center.lat +
                    "&zoom=" + zoom + "&width=" + size.width + '&height=' + size.height + "&markers=" + point.lng + ',' + point.lat;
            editor.execCommand('inserthtml', '<img width="'+ size.width +'"height="'+ size.height +'" src="' + url + '"' + (styleStr ? ' style="' + styleStr + '"' :'') + '/>');
        }
    };
    document.getElementById("address").focus();
</script>


</body>
</html>

show.html文件修改后:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8"/>
    <meta name="keywords" content="百度地图,百度地图API,百度地图自定义工具,百度地图所见即所得工具"/>
    <meta name="description" content="百度地图API自定义地图,帮助用户在可视化操作下生成百度地图"/>
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
    <title>百度地图API自定义地图</title>
    <!--引用百度地图API-->
    <style type="text/css">
        html, body {
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
    </style>
    <!-- <script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=1.1&services=true"></script> -->
    <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=百度地图秘钥&s=1"></script>
</head>

<body onload="initMap();">
<!--百度地图容器-->
<div style="width:697px;height:550px;border:#ccc solid 1px;" id="dituContent"></div>
</body>
<script type="text/javascript">
    function getParam(name) {
        return location.href.match(new RegExp('[?#&]' + name + '=([^?#&]+)', 'i')) ? RegExp.$1 : '';
    }
    var map, marker;
    var centerParam = getParam('center');
    var zoomParam = getParam('zoom');
    var widthParam = getParam('width');
    var heightParam = getParam('height');
    var markersParam = getParam('markers');
    var markerStylesParam = getParam('markerStyles');

    //创建和初始化地图函数:
    function initMap() {
        // [FF]切换模式后报错
        if (!window.BMap) {
            return;
        }
        var dituContent = document.getElementById('dituContent');
        dituContent.style.width = widthParam + 'px';
        dituContent.style.height = heightParam + 'px';

        createMap();//创建地图
        setMapEvent();//设置地图事件
        addMapControl();//向地图添加控件

        // 创建标注
        var markersArr = markersParam.split(',');
        var point = new BMap.Point(markersArr[0], markersArr[1]);
        marker = new BMap.Marker(point);
        marker.enableDragging();
        map.addOverlay(marker); // 将标注添加到地图中

        if(parent.editor && parent.document.body.contentEditable=="true") { //在编辑状态下
            setMapListener();//地图改变修改外层的iframe标签src属性
        }
    }

    //创建地图函数:
    function createMap() {
        map = new BMap.Map("dituContent");//在百度地图容器中创建一个地图
        var centerArr = centerParam.split(',');
        var point = new BMap.Point(parseFloat(centerArr[0]), parseFloat(centerArr[1]));//定义一个中心点坐标
        map.centerAndZoom(point, parseInt(zoomParam));//设定地图的中心点和坐标并将地图显示在地图容器中
    }

    //地图事件设置函数:
    function setMapEvent() {
        map.enableDragging();//启用地图拖拽事件,默认启用(可不写)
        map.enableScrollWheelZoom();//启用地图滚轮放大缩小
        map.enableDoubleClickZoom();//启用鼠标双击放大,默认启用(可不写)
        map.enableKeyboard();//启用键盘上下左右键移动地图
    }

    //地图控件添加函数:
    function addMapControl() {
        //向地图中添加缩放控件
        var ctrl_nav = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_LARGE});
        map.addControl(ctrl_nav);
        //向地图中添加缩略图控件
        var ctrl_ove = new BMap.OverviewMapControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, isOpen: 1});
        map.addControl(ctrl_ove);
        //向地图中添加比例尺控件
        var ctrl_sca = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});
        map.addControl(ctrl_sca);
    }

    function setMapListener() {
        var editor = parent.editor, containerIframe,
            iframes = parent.document.getElementsByTagName('iframe');
        for (var key in iframes) {
            if (iframes[key].contentWindow == window) {
                containerIframe = iframes[key];
                break;
            }
        }
        if (containerIframe) {
            map.addEventListener('moveend', mapListenerHandler);
            map.addEventListener('zoomend', mapListenerHandler);
            marker.addEventListener('dragend', mapListenerHandler);
        }

        function mapListenerHandler() {
            var zoom = map.getZoom(),
                center = map.getCenter(),
                //marker = window.marker.getPoint();
            marker = window.marker.getPosition();
            containerIframe.src = containerIframe.src.
                replace(new RegExp('([?#&])center=([^?#&]+)', 'i'), '$1center=' + center.lng + ',' + center.lat).
                replace(new RegExp('([?#&])markers=([^?#&]+)', 'i'), '$1markers=' + marker.lng + ',' + marker.lat).
                replace(new RegExp('([?#&])zoom=([^?#&]+)', 'i'), '$1zoom=' + zoom);
            editor.fireEvent('saveScene');
        }
    }
</script>
</html>

注意将以上两个文件中的 【百度地图秘钥】修改为 【在百度地图中申请的key】

申请百度地图秘钥

网址: http://lbs.baidu.com/apiconsole/key
点击创建应用,填写应用名称,应用类型选择浏览器端,填写Referer白名单,填写方法详见文本框下方
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值