农燊高科官方网站
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.

location.html 7.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  5. <meta name="keywords" content="天地图" />
  6. <title>天地图-地图API-web定位并获取详细地址</title>
  7. <script type="text/javascript" src="https://api.tianditu.gov.cn/api?v=4.0&tk=cc4aba6e967096098249efa069733067"></script>
  8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  9. <style type="text/css">
  10. body,
  11. html {
  12. width: 100%;
  13. height: 100%;
  14. margin: 0;
  15. font-family: "Microsoft YaHei"
  16. }
  17. #mapDiv {
  18. width: 100%;
  19. height: 400px
  20. }
  21. input,
  22. b,
  23. p {
  24. margin-left: 5px;
  25. font-size: 14px
  26. }
  27. </style>
  28. </head>
  29. <body onLoad="onLoad()">
  30. <div id="mapDiv"></div>
  31. <p>用H5定位所在位置</p>
  32. <span id="localInfo"></span><br/>
  33. <span id="zuobiao"></span><br/>
  34. <div style="width:84%;background:#CCFF66; text-color:#F00; font-size:50px; text-align:center; margin:auto;" onClick="closeWindow();">点击返回</div>
  35. <script>
  36. var map;
  37. //天地图key
  38. const mapKey = "cc4aba6e967096098249efa069733067";
  39. //初始化地图级别
  40. var zoom = 12;
  41. //当前位置
  42. var detaillocation;
  43. //返回坐标
  44. var textZb;
  45. //加载地图
  46. function onLoad() {
  47. //初始化地图对象
  48. map = new T.Map("mapDiv");
  49. //设置显示地图的中心点和级别
  50. map.centerAndZoom(new T.LngLat(116.40969, 38.89945), zoom);
  51. //创建地图类型控件对象
  52. var _mapType = new T.Control.MapType();
  53. //添加地图类型控件
  54. map.addControl(_mapType);
  55. //创建缩放平移控件对象
  56. _zoomControl = new T.Control.Zoom();
  57. //添加缩放平移控件
  58. map.addControl(_zoomControl);
  59. //创建缩放平移控件对象
  60. _zoomControl.setPosition(T_ANCHOR_TOP_LEFT);
  61. //创建定位对象lo
  62. var lo = new T.Geolocation();
  63. //创建右键菜单对象
  64. var menu = new T.ContextMenu({
  65. width: 140
  66. });
  67. //添加右键菜单
  68. var txtMenuItem = [{
  69. text: '放大',
  70. callback: function() {
  71. map.zoomIn()
  72. }
  73. },
  74. {
  75. text: '缩小',
  76. callback: function() {
  77. map.zoomOut()
  78. }
  79. },
  80. {
  81. text: '放置到最大级',
  82. callback: function() {
  83. map.setZoom(18)
  84. }
  85. },
  86. {
  87. text: '查看全国',
  88. callback: function() {
  89. map.setZoom(4)
  90. }
  91. },
  92. {
  93. text: '获得右键点击处坐标',
  94. isDisable: false,
  95. callback: function(lnglat) {
  96. alert(lnglat.getLng() + "," + lnglat.getLat());
  97. }
  98. }
  99. ];
  100. for (var i = 0; i < txtMenuItem.length; i++) {
  101. //添加菜单项
  102. var item = new T.MenuItem(txtMenuItem[i].text, txtMenuItem[i].callback);
  103. //item.disable();
  104. menu.addItem(item);
  105. if (i == 1 || i == 3) {
  106. //添加分割线
  107. menu.addSeparator();
  108. }
  109. }
  110. //装载菜单
  111. map.addContextMenu(menu);
  112. //定位回调函数
  113. fn = function(e) {
  114. //alert(JSON.stringify(e));
  115. //当前为移动端时
  116. if (this.getStatus() == 0) {
  117. textZb = (e.lnglat);
  118. console.log(textZb);
  119. console.log("手机端:"+e);
  120. //获取地理位置信息并设置到标注
  121. getDetailLocation(e.lnglat, e.lnglat);
  122. }
  123. //当前为PC端时
  124. if (this.getStatus() == 1) {
  125. map.centerAndZoom(e.lnglat, e.level)
  126. console.log(e);
  127. //获取地理位置信息并设置到标注
  128. getDetailLocation("PC端:"+e.lnglat, e.lnglat);
  129. }
  130. }
  131. //设置标注
  132. function setMarker(e) {
  133. var marker = new T.Marker(e);
  134. map.addOverLay(marker);
  135. var markerInfoWin = new T.InfoWindow("" + detaillocation);
  136. marker.addEventListener("click", function() {
  137. marker.openInfoWindow(markerInfoWin);
  138. });
  139. }
  140. //通过经纬度获取详细地址
  141. function getDetailLocation(lnglat_lng, lnglat_lat) {
  142. $.ajax({
  143. url: "https://api.tianditu.gov.cn/geocoder",
  144. type: 'GET',
  145. contentType: "application/json;charset=utf-8",
  146. data: {
  147. tk: mapKey,
  148. type: "geocode",
  149. postStr: "{'lon':" + lnglat_lng.lng + ",'lat':" + lnglat_lat.lat + ",'ver':1}"
  150. },
  151. success: function(data) {
  152. var addressdata = eval("(" + data + ")");
  153. //截取地址信息显示在span上
  154. detaillocation = addressdata.result.formatted_address;
  155. $("#localInfo").text(detaillocation + "->" + (new Date()).toLocaleDateString());
  156. $("#zuobiao").text("坐标->"+lnglat_lng.lng+","+lnglat_lat.lat);
  157. console.log(detaillocation);
  158. if (addressdata.msg == "ok" && addressdata.status == 0) {
  159. //将位置信息设置到标注
  160. setMarker(lnglat_lat);
  161. } else {
  162. //dosomething
  163. }
  164. },
  165. error: function(er, er1, er2) {
  166. alert("获取失败");
  167. }
  168. });
  169. }
  170. //开始定位
  171. lo.getCurrentPosition(fn);
  172. }
  173. function closeWindow()
  174. {
  175. if(window.parent) // 跨域iframe传递数据
  176. {
  177. window.parent.postMessage(`LOCATION:${textZb ? JSON.stringify(textZb) : ''}`, '*');
  178. }
  179. console.log("返回");
  180. console.log(textZb);
  181. let chars = window.location.href.split("exiturl=")[1] || "";
  182. console.log(chars);
  183. location.href = `${chars}&lat=${textZb.lat}&lng=${textZb.lng}`;
  184. //window.opener.document.xxxxxx.value=text.value;
  185. window.close();
  186. }
  187. </script>
  188. </body>
  189. </html>