test4.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>点击地图获取地址和经纬度map,address,lng,lat</title>
  5. <meta name="robots" content="noindex, nofollow">
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  7. <!-- 将百度地图API引入,设置好自己的key -->
  8. <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=4FjGxlxzQrIkl7BxwMPl6da5yMxsXwp4"></script>
  9. </head>
  10. <body>
  11. <div class="main-div">
  12. <form method="post" action="" name="theForm" enctype="multipart/form-data" onsubmit="return validate()">
  13. <table cellspacing="1" cellpadding="3" width="100%">
  14. <tr>
  15. <td class="label">经度</td>
  16. <td><input type="text" name="lng" id="lng" value=""/>
  17. </td>
  18. </tr>
  19. <tr>
  20. <td class="label">纬度</td>
  21. <td><input type="text" name="lat" id="lat" value=""/>
  22. </td>
  23. </tr>
  24. <tr>
  25. <td class="label">地址</td>
  26. <td>
  27. <input type='text' value='' name='sever_add' id='sever_add'>
  28. <input type='button' value='点击显示地图获取地址经纬度' id='open'>
  29. </td>
  30. </tr>
  31. </table>
  32. </form>
  33. <div id='allmap' style='width: 50%; height: 50%; position: absolute; display: none'></div>
  34. </div>
  35. </body>
  36. </html>
  37. <script type="text/javascript">
  38. function validate() {
  39. var sever_add = document.getElementsByName('sever_add')[0].value;
  40. if (isNull(sever_add)) {
  41. alert('请选择地址');
  42. return false;
  43. }
  44. return true;
  45. }
  46. //判断是否是空
  47. function isNull(a) {
  48. return (a == '' || typeof(a) == 'undefined' || a == null) ? true : false;
  49. }
  50. document.getElementById('open').onclick = function () {
  51. if (document.getElementById('allmap').style.display == 'none') {
  52. document.getElementById('allmap').style.display = 'block';
  53. } else {
  54. document.getElementById('allmap').style.display = 'none';
  55. }
  56. }
  57. var map = new BMap.Map("allmap");
  58. var geoc = new BMap.Geocoder(); //地址解析对象
  59. var markersArray = [];
  60. var geolocation = new BMap.Geolocation();
  61. var point = new BMap.Point(116.331398, 39.897445);
  62. map.centerAndZoom(point, 12); // 中心点
  63. geolocation.getCurrentPosition(function (r) {
  64. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  65. var mk = new BMap.Marker(r.point);
  66. map.addOverlay(mk);
  67. map.panTo(r.point);
  68. map.enableScrollWheelZoom(true);
  69. }
  70. else {
  71. alert('failed' + this.getStatus());
  72. }
  73. }, {enableHighAccuracy: true})
  74. map.addEventListener("click", showInfo);
  75. //清除标识
  76. function clearOverlays() {
  77. if (markersArray) {
  78. for (i in markersArray) {
  79. map.removeOverlay(markersArray[i])
  80. }
  81. }
  82. }
  83. //地图上标注
  84. function addMarker(point) {
  85. var marker = new BMap.Marker(point);
  86. markersArray.push(marker);
  87. clearOverlays();
  88. map.addOverlay(marker);
  89. }
  90. //点击地图时间处理
  91. function showInfo(e) {
  92. document.getElementById('lng').value = e.point.lng;
  93. document.getElementById('lat').value = e.point.lat;
  94. geoc.getLocation(e.point, function (rs) {
  95. var addComp = rs.addressComponents;
  96. var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
  97. if (confirm("确定要地址是" + address + "?")) {
  98. document.getElementById('allmap').style.display = 'none';
  99. document.getElementById('sever_add').value = address;
  100. }
  101. });
  102. addMarker(e.point);
  103. }
  104. </script>