Google Maps Draw -- 通过拖动绘制线或多边形 [英] Google Maps Draw -- draw line or polygon by dragging

查看:27
本文介绍了Google Maps Draw -- 通过拖动绘制线或多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,您可以通过单击在地图上绘制多边形或折线,这会创建一个节点.但是,如果您想跟随河流或海岸线等要素,这可能既乏味又不准确.

Currently, you can draw polygons or polylines on a map by clicking, which creates a node. However, if you wanted to follow a feature such as a river or shoreline, this could be both tedious and innacurate.

有谁知道可以通过单击拖动鼠标或单击开始,然后拖动路径,然后单击停止来绘制路径的方法吗?

Does anyone know of a way where a path could be drawn either by click-dragging the mouse, or by clicking to start, then dragging a path, then clicking to stop?

我已经查看了 DrawingManager 和 MouseEvent,但我还没有看到任何有希望的东西.

I've looked at the DrawingManager and MouseEvent, but I'm not seeing anything promising yet.

有没有办法根据单击和鼠标移动等事件来连接绘图?

Is there a way to wire up drawing based on events such as click and mouseMove?

这将允许更准确和快速的功能.

This would allow for much more accurate and quick features.

推荐答案

可能的工作流程:

  1. onmousedown在地图上设置地图的draggable-option为false,创建一个Polylinecode>-Instance 并将Polylineclickable-option 设置为false

  1. onmousedown on the map set the draggable-option of the map to false, create a Polyline-Instance and set the clickable-option of the Polyline to false

观察地图的mousemove-事件,每次发生时将返回的LatLng推送到Polyline的路径

observe the mousemove-event of the map, each time it occurs push the returned LatLng to the path of the Polyline

onmouseup 移除地图的 mousemove-listener 并将地图的 draggable-option 设置回 真的.你的 Polyline 已经完成

onmouseup remove the mousemove-listener for the map and set the draggable-option of the map back to true. Your Polyline has been finished

当你不想创建Polygon时,现在创建一个Polygon-instance,设置Polygon的路径到 Polyline 的路径并删除 Polyline

When you wan't to create a Polygon, create now a Polygon-instance, set the path of the Polygon to the path of the Polyline and remove the Polyline

<小时>

:建议只使用鼠标右键进行绘制,否则地图将永远无法拖动.


<edit>: It's recommendable to draw only with a pressed right mousebutton, otherwise the map would never be draggable.

演示:http://jsfiddle.net/doktormolle/YsQdh/一个>

代码片段:(来自链接的 jsfiddle)

code snippet: (from linked jsfiddle)

function initialize() {
  var mapOptions = {
    zoom: 14,
    center: new google.maps.LatLng(52.5498783, 13.425209099999961),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map_canvas'),
    mapOptions);
  google.maps.event.addDomListener(map.getDiv(), 'mousedown', function(e) {
    //do it with the right mouse-button only
    if (e.button != 2) return;
    //the polygon
    poly = new google.maps.Polyline({
      map: map,
      clickable: false
    });
    //move-listener
    var move = google.maps.event.addListener(map, 'mousemove', function(e) {
      poly.getPath().push(e.latLng);
    });
    //mouseup-listener
    google.maps.event.addListenerOnce(map, 'mouseup', function(e) {
      google.maps.event.removeListener(move);
      if (document.getElementById('overlay').value == 'Polygon') {
        var path = poly.getPath();
        poly.setMap(null);
        poly = new google.maps.Polygon({
          map: map,
          path: path
        });
      }
    });

  });
}

google.maps.event.addDomListener(window, 'load', initialize);

html,
body {
  height: 100%;
  margin: 0
}
#map_canvas {
  height: 90%;
}

<script src="https://maps.googleapis.com/maps/api/js"></script>
Use the right mouse-button to draw an overlay
<br/>
<select id="overlay">
  <option value="Polyline">Polyline</option>
  <option value="Polygon">Polygon</option>
</select>
<div id="map_canvas"></div>

这篇关于Google Maps Draw -- 通过拖动绘制线或多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆