当前位置:Gxlcms > html代码 > 关于高德地图WEB版基础控件的展示

关于高德地图WEB版基础控件的展示

时间:2021-07-01 10:21:17 帮助过:31人阅读

这篇文章主要介绍了关于高德地图WEB版基础控件的展示,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

最近学习了一下高德地图web版的使用,总结了一下高德基础控件的应用,高德地图是开源可编辑的,方法不是很难,有兴趣的朋友可以试一试自己编辑一下.

之前想自己做一个旅游导航的项目,在网上一搜发现了高德地图开放平台,发现原来高德可以很简单的就应用到自己的项目里面,当即我就申请了一个key来学一学,仔细研究了一下,感觉还挺难的,网上找了找案例什么的,经过这几天,小编把高德的一些基础控件差不多弄了一下,效果图如下图所示:

废话不多说,直接上源码,下面是js代码:

  1. <script language="javascript" src="http://webapi.amap.com/maps?v=1.2&key=3c5ca12a5778fde874e9959c7fbdf516">//引入高德地图API</script>
  2. <script language="javascript">
  3. var mapObj;
  4. var scale;
  5. var mapType;
  6. var toolBar;
  7. var overView;
  8. var circleEditor;
  9. var circle;
  10. var polygonEditor;
  11. var polygon;
  12. var homeControl;
  13. var controlUI;
  14. var ruler;
  15. var mousetool;
  16. //刷新页面
  17. function reload(){
  18. location.reload();
  19. }
  20. function mapInit(){
  21. mapObj = new AMap.Map("iCenter",{
  22. center:new AMap.LngLat(116.397728,39.90423), //地图中心点
  23. level:13, //地图显示的比例尺级别
  24. });
  25. mapObj.plugin(["AMap.ToolBar"],function(){ //在地图中添加ToolBar插件
  26. toolBar = new AMap.ToolBar();
  27. mapObj.addControl(toolBar);
  28. });
  29. mapObj.plugin(["AMap.Scale"],function(){ //加载比例尺插件
  30. scale = new AMap.Scale();
  31. mapObj.addControl(scale);
  32. scale.show();
  33. });
  34. mapObj.plugin(["AMap.OverView"],function(){ //在地图中添加鹰眼插件
  35. //加载鹰眼
  36. overView = new AMap.OverView({
  37. visible:true //初始化显示鹰眼
  38. });
  39. mapObj.addControl(overView);
  40. overView.open(); //展开鹰眼
  41. });
  42. mapObj.plugin(["AMap.RangingTool"],function(){
  43. ruler = new AMap.RangingTool(mapObj);
  44. AMap.event.addListener(ruler,"end",function(e){
  45. ruler.turnOff();
  46. });
  47. });
  48. mapObj.plugin(["AMap.MouseTool"],function(){
  49. //鼠标工具插件
  50. mousetool = new AMap.MouseTool(mapObj);
  51. });
  52. }
  53. function Coordinate(){
  54. AMap.event.addListener(mapObj,'click',getLnglat); //点击事件
  55. }
  56. function toolBarShow(){
  57. toolBar.show();
  58. toolBar.showRuler();
  59. toolBar.showDirection();
  60. }
  61. function toolBarDirection(){
  62. toolBar.show();
  63. toolBar.showDirection();
  64. toolBar.hideRuler();
  65. }
  66. function toolBarLong(){
  67. toolBar.show();
  68. toolBar.hideDirection();
  69. toolBar.showRuler();
  70. }
  71. function toolBarShot(){
  72. toolBar.show();
  73. toolBar.hideRuler();
  74. toolBar.hideDirection();
  75. }
  76. function iMapType(){
  77. mapObj.plugin(["AMap.MapType"],function(){ //添加地图类型切换插件
  78. //地图类型切换
  79. mapType= new AMap.MapType({defaultType:1,showRoad:true});
  80. mapObj.addControl(mapType);
  81. });
  82. }
  83. function removeMapType(){
  84. mapObj.removeControl(mapType);
  85. }
  86. function iCircleEditor(){ //圆形编辑器
  87. circle = new AMap.Circle({ //圆形编辑器的样式
  88. map: mapObj,
  89. center:new AMap.LngLat("116.40332221984863","39.90025505675715"),
  90. radius:1000,
  91. strokeColor: "#F33",
  92. strokeOpacity: 1,
  93. strokeWeight: 3,
  94. fillColor: "ee2200",
  95. fillOpacity: 0.35
  96. });
  97. mapObj.plugin(["AMap.CircleEditor"],function(){
  98. circleEditor = new AMap.CircleEditor(mapObj,circle); //创建圆形编辑器对象
  99. circleEditor.open(); //打开圆形编辑器
  100. });
  101. }
  102. function removeCicleEditor(){ //关闭圆形编辑器,隐藏圆形
  103. circleEditor.close();
  104. circle.hide();
  105. }
  106. function iPloygonEditor(){ //编辑多边形
  107. var arr=new Array();//经纬度坐标数组
  108. arr.push(new AMap.LngLat("116.403322","39.920255"));
  109. arr.push(new AMap.LngLat("116.410703","39.897555"));
  110. arr.push(new AMap.LngLat("116.402292","39.892353"));
  111. arr.push(new AMap.LngLat("116.389846","39.891365"));
  112. polygon = new AMap.Polygon({
  113. path:arr, //设置多边形轮廓的节点数组
  114. strokeColor:"#0000ff",
  115. strokeOpacity:0.2,
  116. strokeWeight:3,
  117. fillColor: "#f5deb3",
  118. fillOpacity: 0.35
  119. });
  120. //地图上添加多边形
  121. mapObj.addOverlays(polygon);
  122. //构造多边形编辑对象,并开启多边形的编辑状态
  123. mapObj.plugin(["AMap.PolyEditor"],function(){
  124. polygonEditor = new AMap.PolyEditor(mapObj,polygon);
  125. polygonEditor.open();
  126. });
  127. }
  128. function removePloygonEditor(){
  129. polygonEditor.close();
  130. polygon.hide();
  131. }
  132. AMap.homeControlp = function(){}
  133. AMap.homeControlp.prototype = {
  134. addTo: function(map, dom){
  135. dom.appendChild(this._getHtmlDom(map));
  136. },
  137. _getHtmlDom:function(map){
  138. this.map=map;
  139. // 创建一个能承载控件的<p>容器
  140. controlUI = document.createElement("p");
  141. controlUI.style.width='80px'; //设置控件容器的宽度
  142. controlUI.style.height='20px'; //设置控件容器的高度
  143. controlUI.style.backgroundColor='white';
  144. controlUI.style.borderStyle='solid';
  145. controlUI.style.borderWidth='2px';
  146. controlUI.style.cursor='pointer';
  147. controlUI.style.textAlign='center';
  148. // 设置控件的位置
  149. controlUI.style.position='absolute';
  150. controlUI.style.left='120px'; //设置控件离地图的左边界的偏移量
  151. controlUI.style.top='5px'; //设置控件离地图上边界的偏移量
  152. controlUI.style.zIndex='300'; //设置控件在地图上显示
  153. // 设置控件字体样式
  154. controlUI.style.fontFamily='Arial,sens-serif';
  155. controlUI.style.fontSize='12px';
  156. controlUI.style.paddingLeft='4px';
  157. controlUI.style.paddingRight='4px';
  158. controlUI.innerHTML="换中心点";
  159. // 设置控件响应点击onclick事件
  160. controlUI.onclick = function(){
  161. map.setCenter(new AMap.LngLat(116.234404, 39.12915));
  162. }
  163. return controlUI;
  164. }
  165. }
  166. AMap.event.trigger(homeControlp,"hide");
  167. AMap.event.addListener(homeControlp,"hide",function(){
  168. controlUI.style.display = 'none';
  169. })
  170. function myControl(){
  171. homeControl = new AMap.homeControlp(mapObj); //新建自定义插件对象
  172. mapObj.addControl(homeControl); //地图上添加插件
  173. }
  174. function removeMyControl(){
  175. homeControl.hide();
  176. //controlUI.style.display='none';
  177. }
  178. function iRangingTool(){
  179. ruler.turnOn();
  180. }
  181. function removeRangingTool(){
  182. ruler.turnOff();
  183. mapObj.clearMap();
  184. //ruler.hide();
  185. //ruler.setMap(null);
  186. //mapObj.removeControl(ruler);
  187. }
  188. function iMarker(){
  189. mousetool.marker(); //使用鼠标工具,在地图上画标记点
  190. }
  191. function iMeasureArea(){
  192. mousetool.measureArea();
  193. }
  194. function iRectZoomIn(){
  195. mousetool.rectZoomIn();
  196. }
  197. function iRectZoomOut(){
  198. mousetool.rectZoomOut();
  199. }
  200. function iPolyline(){
  201. mousetool.polyline();
  202. }
  203. function iPolygon(){
  204. mousetool.polygon();
  205. }
  206. function iCircle(){
  207. mousetool.circle();
  208. }
  209. function iRectangle(){
  210. mousetool.rectangle();
  211. }
  212. function iRule(){
  213. mousetool.rule();
  214. }
  215. function removeMouseTool(){
  216. mousetool.close(true);
  217. }
  218. function geocoder() {
  219. var MGeocoder;
  220. //加载地理编码插件
  221. mapObj.plugin(["AMap.Geocoder"], function() {
  222. MGeocoder = new AMap.Geocoder({
  223. radius: 1000,
  224. extensions: "all"
  225. });
  226. //返回地理编码结果
  227. AMap.event.addListener(MGeocoder, "complete", geocoder_CallBack);
  228. //逆地理编码
  229. MGeocoder.getAddress(lnglatXY);
  230. });
  231. //加点
  232. var marker = new AMap.Marker({
  233. map:mapObj,
  234. icon: new AMap.Icon({
  235. image: "http://api.amap.com/Public/images/js/mark.png",
  236. size:new AMap.Size(58,30),
  237. imageOffset: new AMap.Pixel(-32, -0)
  238. }),
  239. position: lnglatXY,
  240. offset: new AMap.Pixel(-5,-30)
  241. });
  242. // mapObj.setFitView();
  243. }
  244. //回调函数
  245. function geocoder_CallBack(data) {
  246. var address;
  247. //返回地址描述
  248. address = data.regeocode.formattedAddress;
  249. //返回结果拼接
输出 document.getElementById("iAddress").innerHTML = address; } //鼠标点击,获取经纬度坐标 function getLnglat(e){ mapObj.clearMap(); var x = e.lnglat.getLng(); var y = e.lnglat.getLat(); document.getElementById("lnglat").innerHTML = x + "," + y; lnglatXY = new AMap.LngLat(x,y); geocoder(); } </script>

下面是HTML代码:

  1. <body onLoad="mapInit()">
  2. <p id="iCenter"></p>
  3. <p id="iControlbox">
  4. <ul>
  5. <li><button onclick="javascript:toolBarShow();">显示完整鱼骨</button><button onclick="javascript:toolBar.hide();">隐藏鱼骨</button><button onclick="javascript:toolBarDirection();">方向盘</button><button onclick="javascript:toolBarLong();">长标尺</button><button onclick="javascript:toolBarShot();">短标尺</button></li>
  6. <li><button onclick="javascript:scale.show();">显示比例尺</button><button onclick="javascript:scale.hide();">隐藏比例尺</button></li>
  7. <li><button onclick="javascript:overView.show();">显示鹰眼</button><button onclick="javascript:overView.hide();">隐藏鹰眼</button></li>
  8. <li><button onclick="javascript:iMapType();">添加地图类型切换</button><button onclick="javascript:removeMapType();">移除地图类型切换</button></li>
  9. <li><button onclick="javascript:iCircleEditor();">添加圆形编辑器</button><button onclick="javascript:removeCicleEditor();">删除圆形编辑器</button></li>
  10. <li><button onclick="javascript:iPloygonEditor();">添加多边形编辑器</button><button onclick="javascript:removePloygonEditor();">删除多边形编辑器</button></li>
  11. <li><button onclick="javascript:iMarker();">鼠标打点工具</button><button onclick="javascript:removeMouseTool();">清除</button></li>
  12. <li><button onclick="javascript:iPolyline();">鼠标画折线工具</button><button onclick="javascript:removeMouseTool();">清除</button></li>
  13. <li><button onclick="javascript:iPolygon();">鼠标画多边形工具</button><button onclick="javascript:removeMouseTool();">清除</button></li>
  14. <li><button onclick="javascript:iCircle();">鼠标画圆形工具</button><button onclick="javascript:removeMouseTool();">清除</button></li>
  15. <li><button onclick="javascript:iRectangle();">鼠标画矩形工具</button><button onclick="javascript:removeMouseTool();">清除</button></li>
  16. <li><button onclick="javascript:iRule();">鼠标测距工具</button><button onclick="javascript:removeMouseTool();">清除</button></li>
  17. <li><button onclick="javascript:iMeasureArea();">鼠标测面积</button><button onclick="javascript:removeMouseTool();">移除</button></li>
  18. <li><button onclick="javascript:iRectZoomIn();">鼠标框选缩小</button><button onclick="javascript:iRectZoomOut();">鼠标框选放大</button><button onclick="javascript:removeRangingTool();">关闭鼠标放大缩小</button></li>
  19. <li><button onclick="javascript:iRangingTool();">测距插件</button><button onclick="javascript:removeRangingTool();">隐藏测距</button></li>
  20. <li><button onclick="javascript:myControl();">添加自定义控件</button><button onclick="javascript:removeMyControl();">移除自定义控件</button></li>
  21. <li><p class="detail"><p><span id="lnglat"> </span></p><p><span id="iAddress"> </span></p></p></li>
  22. <li><button onclick="javascript:Coordinate();">坐标拾取控件</button><button onclick="javascript:reload();">取消坐标拾取</button></li>
  23. </ul>
  24. </p>
  25. </body>

在js第一行引入高德地图API中,key值是我自己在高德地图里面创建的也可以用,你们也可以上高德开放平台自行申请key值试一试。

高德开放平台:developer.amap.com/

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

html用表格布局来实现用户注册表单实例

点击按钮文字变成input框,点击保存变成文字的实现

以上就是关于高德地图WEB版基础控件的展示的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行