首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >必应映射v8 JS

必应映射v8 JS
EN

Stack Overflow用户
提问于 2017-05-05 11:08:11
回答 1查看 4.7K关注 0票数 2

我有一个非常简单的Bing地图程序,我想让用户在地图上画一个形状,我有绘图工具和一切设置,但是Bing的事件似乎没有以正确的方式触发-

开始绘图-当我将绘图工具更改为直线或多边形时触发。

绘图更改-当我将绘图工具更改为直线或多边形时触发

当我开始绘制一个新的多边形时,我只想清除映射中的现有多边形--但是对绘图管理器上的getPrimitives函数进行调用会清除绘图模式,因此我考虑缓存绘图模式,读取原语删除它们,然后重置绘图模式,然后绘图管理器上的setDrawingMode方法再次调用绘图,这再次触发整个过程。

有谁知道怎么克服这个问题吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-05 16:38:06

当您单击该模式时,绘图已启动事件看起来确实很奇怪。会让团队调查的。

然而,你想要做的事情会有一些潜在的问题。如果在用户开始在地图上绘制多边形时清除绘图管理器,则该多边形也将从地图中移除。您可以做的是,当完成绘制一个多边形,是将它移动到一个单独的层,然后您可以清除该层而不影响绘图管理器。如果您只对绘制多边形感兴趣,您甚至不需要绘图管理器,因为您可以自己使用绘图工具和按钮来处理这个问题。例如:CreateShape

下面是使用绘图管理器实现所需内容的代码示例:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type='text/javascript'>
    var map, baseLayer, drawingManager;

    function GetMap() {
        map = new Microsoft.Maps.Map('#myMap', {
            credentials: 'YourBingMapsKey'
        });

        //Load the DrawingTools module
        Microsoft.Maps.loadModule('Microsoft.Maps.DrawingTools', function () {
            //Create a base layer to add drawn shapes.
            baseLayer = new Microsoft.Maps.Layer();
            map.layers.insert(baseLayer);

            //Create an instance of the DrawingTools class and bind it to the map.
            var tools = new Microsoft.Maps.DrawingTools(map);

            //Show the drawing toolbar and enable editting on the map.
            tools.showDrawingManager(function (manager) {
                drawingManager = manager;

                Microsoft.Maps.Events.addHandler(drawingManager, 'drawingEnded', function (e) {
                    //When use finisihes drawing a shape, removing it from the drawing layer and add it to the base layer.
                    var shapes = drawingManager.getPrimitives();

                    if (shapes) {
                        drawingManager.clear();
                        baseLayer.add(shapes);
                    }
                });

                Microsoft.Maps.Events.addHandler(drawingManager, 'drawingChanging', function (e) {
                    //When the drawing is changing, clear the base layer.
                    baseLayer.clear();
                });
            })
        });
    }
    </script>
    <script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>
</head>
<body>
    <div id="myMap" style="position:relative;width:600px;height:400px;"></div>
</body>
</html>

下面是一个类似的代码示例,它不需要绘图管理器和自定义按钮就可以开始绘制多边形。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type='text/javascript'>
    var map, baseLayer, tools, currentShape;

    function GetMap() {
        map = new Microsoft.Maps.Map('#myMap', {
            credentials: 'YourBingMapsKey'
        });

        //Create a base layer to add drawn shapes.
        baseLayer = new Microsoft.Maps.Layer();
        map.layers.insert(baseLayer);

        //Load the DrawingTools module.
        Microsoft.Maps.loadModule('Microsoft.Maps.DrawingTools', function () {
            //Create an instance of the DrawingTools class and bind it to the map.
            tools = new Microsoft.Maps.DrawingTools(map);

            Microsoft.Maps.Events.addHandler(tools, 'drawingChanging', function (e) {
                //When the drawing is changing, clear the base layer.
                baseLayer.clear();
            });


            //When the user presses 'esc', take the polygon out of edit mode and re-add to base map.
            document.getElementById('myMap').onkeypress = function (e) {
                if (e.charCode === 27) {
                    tools.finish(shapeDrawn);
                    currentShape = null;
                }
            };
        });
    }

    function drawPolygon() {
        //Stop any current drawing.
        if (currentShape) {
            tools.finish(shapeDrawn);
            currentShape = null;
        }

        //Create a new polygon.
        tools.create(Microsoft.Maps.DrawingTools.ShapeType.polygon, function (s) {
            currentShape = s;
        });
    }

    function shapeDrawn(s) {
        baseLayer.add(s);
    }
    </script>
    <script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>
</head>
<body>
    <div id="myMap" style="position:relative;width:600px;height:400px;"></div><br />
    <input type="button" onclick="drawPolygon()" value="Draw Polygon" />
</body>
</html>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43803421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档