test.html 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  7. <style>
  8. html,
  9. body,
  10. #container {
  11. width: 100%;
  12. height: 100%;
  13. }
  14. </style>
  15. <title>多边形的绘制和编辑</title>
  16. <script type="text/javascript">
  17. window._AMapSecurityConfig = {
  18. securityJsCode: "31df7baead90ccb00fa8646c4de036df",
  19. }
  20. </script>
  21. <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
  22. <script src="https://webapi.amap.com/maps?v=2.0&key=c0ac61673a7331ca7750f916768930dc&plugin=AMap.CircleEditor,AMap.Geocoder,AMap.AutoComplete,AMap.PlaceSearch,AMap.PolygonEditor,AMap.MouseTool"></script>
  23. <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
  24. </head>
  25. <body>
  26. <div id="container"></div>
  27. <div class="input-card" style="width: 120px">
  28. <button class="btn" onclick="polyEditor.open()" style="margin-bottom: 5px">开始编辑</button>
  29. <button class="btn" onclick="polyEditor.close()">结束编辑</button>
  30. </div>
  31. <script type="text/javascript">
  32. var map = new AMap.Map("container", {
  33. center: [116.400274, 39.905812],
  34. zoom: 14
  35. });
  36. var path = [
  37. [116.403322, 39.920255],
  38. [116.410703, 39.897555],
  39. [116.402292, 39.892353],
  40. [116.389846, 39.891365]
  41. ]
  42. var polygon = new AMap.Polygon({
  43. path: path,
  44. strokeColor: "#FF33FF",
  45. strokeWeight: 6,
  46. strokeOpacity: 0.2,
  47. fillOpacity: 0.4,
  48. fillColor: '#1791fc',
  49. zIndex: 50,
  50. })
  51. map.add(polygon)
  52. // 缩放地图到合适的视野级别
  53. map.setFitView([ polygon ])
  54. var polyEditor = new AMap.PolyEditor(map, polygon)
  55. polyEditor.on('addnode', function(event) {
  56. log.info('触发事件:addnode')
  57. })
  58. polyEditor.on('adjust', function(event) {
  59. log.info('触发事件:adjust')
  60. })
  61. polyEditor.on('removenode', function(event) {
  62. log.info('触发事件:removenode')
  63. })
  64. polyEditor.on('end', function(event) {
  65. log.info('触发事件: end')
  66. // event.target 即为编辑后的多边形对象
  67. })
  68. </script>
  69. </body>
  70. </html>