天波h5前端应用
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

152 linhas
4.8KB

  1. <!doctype html>
  2. <html lang="">
  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. <title>高德地图demo</title>
  8. <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" type="text/css">
  9. <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=6e4a6c39ea6d18b8dd3151baa3a7c0d5"></script>
  10. <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
  11. <style>
  12. html,body,#container{
  13. height: 100%
  14. }
  15. .info {
  16. height: 100px;
  17. width: 250px;
  18. z-index: 9999;
  19. position: absolute;
  20. top: 20px;
  21. left: 20%;
  22. background-color: wheat;
  23. text-align: center;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container"></div>
  29. <div class='info'>
  30. <p>拖动marker可调整位置</p>
  31. <input type="text" id="text"/>
  32. </div>
  33. <script script type="text/javascript">
  34. /* var textBox = new AMap.Text({
  35. map: map,
  36. position: new AMap.LngLat(116.40109,39.919728),
  37. offset: new AMap.Pixel(-20, -40),
  38. style:{
  39. 'background-color':'yellow',
  40. 'border':'solid 1px #0088ff',
  41. 'padding':'10px 20px'
  42. }
  43. }) */
  44. // 获取url的参数
  45. function getQueryString() {
  46. var url = window.location.hash;
  47. console.log(url); //获取url中"?"符后的字串
  48. let theRequest = new Object();
  49. if (url.indexOf("?") != -1) {
  50. let str = url.substr(8);
  51. console.log("str", str);
  52. strs = str.split("&");
  53. for(let i = 0; i < strs.length; i ++) {
  54. theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
  55. }
  56. }
  57. return theRequest;
  58. }
  59. var params = getQueryString();
  60. console.log(params.polygon);
  61. var polyLnglAtList = params.polygon.split(';').map(item => {
  62. return item.split(',');
  63. });
  64. var radius = Number(params.radius);
  65. console.log("radius", radius);
  66. var center = params.center.split(',');
  67. var map = new AMap.Map("container", {
  68. resizeEnable: true,
  69. zoom: 13,
  70. center: center
  71. });
  72. console.log("polyLnglAtList", polyLnglAtList);
  73. var marker = new AMap.Marker({
  74. map: map,
  75. draggable:true,
  76. position: center
  77. });
  78. function createPoly() {
  79. // 创建地图
  80. // 判断是多边形还是圆形 数组length为1 是圆形
  81. if (polyLnglAtList.length === 1) {
  82. // 创建点
  83. var polygon = new AMap.Circle({
  84. center: center,
  85. radius: radius, //半径
  86. borderWeight: 3,
  87. strokeColor: "red",
  88. strokeWeight: 6,
  89. strokeOpacity: 0.5,
  90. fillOpacity: 0.4,
  91. strokeStyle: 'dashed',
  92. strokeDasharray: [10, 10],
  93. // 线样式还支持 'dashed'
  94. fillColor: 'green',
  95. zIndex: 50,
  96. });
  97. map.add(polygon)
  98. return polygon
  99. } else {
  100. // 否则是多边形
  101. var polygon = new AMap.Polygon({
  102. map: map,
  103. fillOpacity: 0.4,
  104. path: polyLnglAtList
  105. });
  106. return polygon
  107. }
  108. };
  109. // 绑定拖拽事件
  110. function compute() {
  111. var point = marker.getPosition();
  112. console.log("当前坐标点::", point);
  113. /* var isPointInRing = AMap.GeometryUtil.isPointInRing(point,polyLnglAtList); */
  114. var newPolygon = createPoly();
  115. console.log("newPolygon", newPolygon);
  116. var isPointInRing = newPolygon.contains(point);
  117. marker.setLabel({
  118. content: isPointInRing ? '在内部'+ point : '在外部' + point,
  119. offset:new AMap.Pixel(20,0)
  120. });
  121. // 双向绑定
  122. var input = document.querySelector("#text");
  123. var data = {};
  124. Object.defineProperty(data, "name", {
  125. configurable: true,
  126. get: function(){
  127. return input.value
  128. },
  129. set: function(newValue){
  130. //this.value = newValue;
  131. input.value = newValue;
  132. }
  133. })
  134. data.name = point.toString();
  135. input.onchange = function(){
  136. data.name = data.name;
  137. };
  138. let divBox = document.createElement('span');
  139. divBox.innerHTML = isPointInRing;
  140. divBox.className = 'fence';
  141. document.body.appendChild(divBox);
  142. console.log("该点是否在围栏内::", isPointInRing);
  143. }
  144. createPoly();
  145. compute();
  146. marker.on('dragging',compute)
  147. map.setFitView();
  148. </script>
  149. <div id="gaode"></div>
  150. </body>
  151. </html>