amap.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <style>
  2. #city-container{
  3. margin:0;
  4. width: 100%;
  5. height: 100%;
  6. background: rgba(0, 0, 0, 0) !important;
  7. }
  8. .map-container {
  9. height: 500px;
  10. width:500px;
  11. position: absolute;
  12. z-index: 9999
  13. }
  14. .tipMarker {
  15. padding-left:20px;
  16. width:200px
  17. }
  18. .amap-logo {
  19. display: none;
  20. opacity: 0 !important;
  21. }
  22. .amap-copyright {
  23. opacity: 0;
  24. }
  25. </style>
  26. <script>
  27. window._AMapSecurityConfig = {
  28. //密钥
  29. securityJsCode:'<?= $secret_code; ?>',
  30. }
  31. </script>
  32. <script language="javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=<?= $secret_key; ?>&plugin=Map3D,AMap.DistrictSearch"></script>
  33. <script src="https://webapi.amap.com/ui/1.1/main.js"></script>
  34. <div class="map-container">
  35. <div id="city-container"></div>
  36. </div>
  37. <script language="javascript">
  38. var opts = {
  39. subdistrict: 0,
  40. extensions: 'all',
  41. level: 'city'
  42. };
  43. //利用行政区查询获取边界构建mask路径
  44. //也可以直接通过经纬度构建mask路径
  45. var district = new AMap.DistrictSearch(opts);
  46. var map;
  47. // console.log(district);
  48. district.search('<?= $value ?>', function(status, result) {
  49. var bounds = result.districtList[0].boundaries;
  50. var mask = []
  51. for(var i =0;i<bounds.length;i+=1){
  52. mask.push([bounds[i]])
  53. }
  54. map = new AMap.Map('city-container', {
  55. mask:mask,
  56. disableSocket:true,
  57. viewMode:'3D',
  58. showLabel:false,
  59. labelzIndex:130,
  60. pitch:20,
  61. zoom:9,
  62. resizeEnable: true,
  63. showIndoorMap: false,
  64. //mapStyle: "", 地图样式
  65. features: ["point", "road", "bg"],
  66. zoomEnable: true
  67. });
  68. initOtherMap(result.districtList[0].adcode);
  69. });
  70. function initOtherMap(adcode) {
  71. AMapUI.load(["ui/geo/DistrictExplorer", "lib/$"], (DistrictExplorer, $) => {
  72. let districtExplorer = new DistrictExplorer({
  73. map: map, //关联的地图实例
  74. eventSupport: true,
  75. });
  76. districtExplorer.loadAreaNode(adcode, (error, areaNode) => {
  77. if (error) {
  78. console.error(error);
  79. return;
  80. }
  81. //绘制载入的区划节点
  82. renderAreaNode(districtExplorer, areaNode);
  83. });
  84. var $tipMarkerContent = $('<div class="tipMarker top"></div>');
  85. var tipMarker = new AMap.Marker({
  86. content: $tipMarkerContent.get(0),
  87. offset: new AMap.Pixel(0, 0),
  88. bubble: true,
  89. });
  90. districtExplorer.on("featureMousemove", function (e, feature) {
  91. // 更新提示位置
  92. tipMarker.setPosition(e.originalEvent.lnglat);
  93. });
  94. districtExplorer.on("featureMouseout featureMouseover", (e, feature) => {
  95. toggleHoverFeature(
  96. districtExplorer,
  97. $tipMarkerContent,
  98. tipMarker,
  99. feature,
  100. e.type === "featureMouseover",
  101. e.originalEvent ? e.originalEvent.lnglat : null,
  102. );
  103. });
  104. });
  105. }
  106. function renderAreaNode(districtExplorer, areaNode) {
  107. //清除已有的绘制内容
  108. districtExplorer.clearFeaturePolygons();
  109. //绘制子级区划
  110. districtExplorer.renderSubFeatures(areaNode, function (feature, i) {
  111. return {
  112. cursor: "default",
  113. bubble: true,
  114. strokeColor: '#0691FF', //线颜色
  115. strokeOpacity: 1, //线透明度
  116. strokeWeight: 0.5, //线宽
  117. fillColor: '#ffffff', //填充色
  118. fillOpacity: 0.5, //填充透明度
  119. };
  120. });
  121. //绘制父级区划,仅用黑色描边
  122. districtExplorer.renderParentFeature(areaNode, {
  123. cursor: "default",
  124. bubble: true,
  125. strokeColor: "#0691FF", //线颜色
  126. fillColor: null,
  127. strokeWeight: 2, //线宽
  128. });
  129. //更新地图视野以适合区划面
  130. map.setFitView(districtExplorer.getAllFeaturePolygons());
  131. }
  132. // 提示框
  133. function toggleHoverFeature(
  134. districtExplorer,
  135. $tipMarkerContent,
  136. tipMarker,
  137. feature,
  138. isHover,
  139. position,
  140. ) {
  141. tipMarker.setMap(isHover ? map : null);
  142. if (!feature) {
  143. return;
  144. }
  145. let props = feature.properties;
  146. if (isHover) {
  147. // 更新提示内容
  148. $tipMarkerContent.html(props.adcode + ": " + props.name);
  149. // 更新位置
  150. tipMarker.setPosition(position || props.center);
  151. }
  152. // 更新相关多边形的样式
  153. let polys = districtExplorer.findFeaturePolygonsByAdcode(props.adcode);
  154. for (let i = 0, len = polys.length; i < len; i++) {
  155. polys[i].setOptions({
  156. fillOpacity: isHover ? 0.5 : 0.5,
  157. fillColor: isHover ? '#0691FF' : '#fff',
  158. });
  159. }
  160. }
  161. </script>
粤ICP备19079148号