简体   繁体   English

是否可以在绘制时取消 Google Maps API 折线?

[英]Is it possible to cancel a Google Maps API polyline while it's being drawn?

I've built a Google Maps API toolset that allows the user to draw shapes over the map, give them names and record areas.我已经构建了一个 Google Maps API 工具集,它允许用户在地图上绘制形状,给它们命名并记录区域。 On the completion of each shape, they are prompted to give it a name and set a few other options such as whether or not to show a label on the map.完成每个形状后,系统会提示他们为其命名并设置一些其他选项,例如是否在地图上显示标签。

I'd like to give the user the option to right click and cancel a polyline (or polygon) whilst placing the points, ie while they're drawing it.我想为用户提供在放置点时右键单击并取消折线(或多边形)的选项,即在绘制点时。

Based on what I've read in the documentation, I should be able to detect that the user right clicked on the map, but I'm not sure how to cancel the overlay they were drawing, as it won't have been committed to the map yet, which means I won't be able to refer to it as an object.根据我在文档中读到的内容,我应该能够检测到用户右键单击了地图,但我不确定如何取消他们正在绘制的叠加层,因为它不会被提交地图,这意味着我将无法将其称为对象。

Any ideas?有任何想法吗?

Solution解决方案

Thanks to Dr Molle for the solution, which was as follows:感谢 Molle 博士的解决方案,内容如下:

    ...
    google.maps.event.addListener(_map, "rightclick", function(){
        InitialiseDrawingManager();
    });
}

function InitialiseDrawingManager(){
    if (_drawingManager != null)
        _drawingManager.setMap(null);

    _drawingManager = new google.maps.drawing.DrawingManager();
    _drawingManager.setMap(_map);

    UpdateOverlaySettings();
    ...

While the code for the accepted answer works, it throws an error, probably due to event listeners not being removed properly.虽然已接受答案的代码有效,但它会引发错误,这可能是由于未正确删除事件侦听器。

The following is a better approach:下面是一个更好的方法:

I used a variable cancelDrawingShape to detect if the Escape button has been pressed while stuff is being drawn.我使用了一个变量cancelDrawingShape来检测在绘制内容时是否按下了 Escape 按钮。 If the cancelDrawingShape is set to true , then I will remove the last drawn shape from the map.如果cancelDrawingShape设置为true ,那么我将从地图中删除最后绘制的形状。

var cancelDrawingShape = false;

google.maps.event.addListener(drawingManager, 'overlaycomplete', function (e) {
    var lastDrawnShape = e.overlay;
    if (cancelDrawingShape) {
        cancelDrawingShape = false;
        lastDrawnShape.setMap(null); // Remove drawn but unwanted shape
        return;
    }

    // Else, do other stuff with lastDrawnShape
});

$(document).keydown(function (event) {
    if (event.keyCode === 27) { // Escape key pressed
        cancelDrawingShape = true;
        drawingManager.setDrawingMode(null); // To terminate the drawing, will result in autoclosing of the shape being drawn.
    }
});

(Assuming you use the google.maps.drawing.DrawingManager ) (假设您使用google.maps.drawing.DrawingManager

The current overlay is not accessible in any manner.无法以任何方式访问当前覆盖。 What you can do:你可以做什么:

On rightclick set the map -property of the DrawingManager -instance to null and create a new DrawingManager -instance. rightclickDrawingManager -instance 的map -property 设置为 null 并创建一个新的DrawingManager -instance。

This will remove the currently edited overlay(but not the already finished overlays)这将删除当前编辑的叠加层(但不会删除已经完成的叠加层)

For Angular 7, This worked for me.对于 Angular 7,这对我有用。

cancelDrawingShape: boolean = false; // declaration of variable
    @HostListener('document:keydown', ['$event']) onKeydownHandler(event: KeyboardEvent) {
        if (event.key === "Escape" || event.key === "Esc") {
          this.cancelDrawingShape = true;
          this.drawingManager.setDrawingMode(null); // To terminate the drawing, will result in autoclosing of the shape being drawn.
        }
      }

        google.maps.event.addListener(this.drawingManager,"polygoncomplete",
          polygon => {
            if(this.cancelDrawingShape){
              this.selectedShape = polygon;
              this.deleteSelectedShape();
              this.cancelDrawingShape = false;
              return false;
            }else{
               // else part
    }});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM