农燊高科官方网站
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

96 lines
3.5 KiB

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="http://api.tianditu.com/api?v=4.0&tk=cc4aba6e967096098249efa069733067"></script>
  7. <script>
  8. var map,control;
  9. function onLoad() {
  10. // 初始化地图对象
  11. map = new T.Map('mapDiv');
  12. // 设置显示地图的中心点和级别
  13. map.centerAndZoom(new T.LngLat(117.985096, 37.3801), 8);
  14. // 允许鼠标滚轮缩放地图
  15. map.enableScrollWheelZoom();
  16. /*
  17. //添加鹰眼控件
  18. var miniMap = new T.Control.OverviewMap({
  19. isOpen: true,
  20. size: new T.Point(150, 150)
  21. });
  22. map.addControl(miniMap);
  23. */
  24. // 创建比例尺控件对象
  25. var scale = new T.Control.Scale();
  26. // 添加比例尺控件
  27. map.addControl(scale);
  28. // 创建缩放平移控件对象
  29. control = new T.Control.Zoom();
  30. // 添加缩放平移控件
  31. map.addControl(control);
  32. control.setPosition(T_ANCHOR_TOP_RIGHT);
  33. // 创建标记图片对象(支持更换图标)
  34. var icon = new T.Icon({
  35. iconUrl: "http://api.tianditu.com/img/map/markerA.png",
  36. iconSize: new T.Point(19, 27),
  37. iconAnchor: new T.Point(10, 25)
  38. });
  39. // 后台要传来的json数据(测试数据)
  40. var jsonStr = "[{'longitude':'117.976101','latitude':'37.418297','address':'滨州','code':'yykj','waterRight':'1300','waterConsumption':'620','state':'0'},{'longitude':'117.985650','latitude':'37.379740','address':'a区','code':'xdgj'}]";
  41. // json串转json对象
  42. var json = eval('(' + jsonStr + ')');
  43. var marker,content;
  44. // 循环获取json数据
  45. for (var i = 0; i < json.length; i++) {
  46. // 创建标注
  47. marker = new T.Marker(new T.LngLat(json[i].longitude,json[i].latitude),{icon: icon});
  48. // 准备标注信息
  49. content = "点击信息";
  50. // 将标注添加到地图中
  51. map.addOverLay(marker);
  52. // 为标注添加点击事件
  53. addClickHandler(content,marker);
  54. }
  55. function addClickHandler(content,marker){
  56. // 为标注添加点击事件
  57. marker.addEventListener("click",function(e){
  58. openInfo(content,e);
  59. }
  60. );}
  61. function openInfo(content,e){
  62. // 获取标注的经纬坐标
  63. var point = e.lnglat;
  64. // 创建标注
  65. marker = new T.Marker(point);
  66. // 创建信息窗口对象
  67. var markerInfoWin = new T.InfoWindow(content,{offset:new T.Point(0,-20)});
  68. // 开启信息窗口
  69. map.openInfoWindow(markerInfoWin,point);
  70. }
  71. }
  72. </script>
  73. </head>
  74. <body onLoad="onLoad()">
  75. <div id="mapDiv" style="position:absolute;width:1400px; height:500px"></div>
  76. </body>
  77. </html>
  78. ————————————————
  79. 版权声明:本文为CSDN博主「爱吃芒果2」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
  80. 原文链接:https://blog.csdn.net/weixin_52072568/article/details/127391217