map.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
  6. <style type="text/css">
  7. body, html, #allmap {
  8. width: 100%;
  9. height: 100%;
  10. overflow: hidden;
  11. margin: 0;
  12. }
  13. #suggestId {
  14. position: absolute;
  15. z-index: 9999;
  16. right: 20px;
  17. top: 20px;
  18. line-height: 32px;
  19. padding: 0 10px;
  20. width: 300px;
  21. border: 1px solid #e4e6e7;
  22. }
  23. #suggestId:focus {
  24. outline: none;
  25. }
  26. </style>
  27. <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=KxkuAcGBup6sD1XxaDW85KBG"></script>
  28. <title>点击地图获取当前经纬度</title>
  29. </head>
  30. <body>
  31. <input id="lng_input" type="hidden" value="{$lng}"/>
  32. <input id="lat_input" type="hidden" value="{$lat}"/>
  33. <input id="address_input" type="hidden"/>
  34. <input id="city_input" type="hidden"/>
  35. <input id="province_input" type="hidden"/>
  36. <input id="district_input" type="hidden"/>
  37. <input id="suggestId" type="text" placeholder="请输入地址"/>
  38. <div id="allmap"></div>
  39. <script type="text/javascript">
  40. // 百度地图API功能
  41. var center = new BMap.Point({$lng}, {$lat});
  42. var map = new BMap.Map("allmap");
  43. map.centerAndZoom(new BMap.Point(116.404, 39.915), 8);
  44. map.enableScrollWheelZoom(); //启用滚轮放大缩小,默认禁用
  45. map.enableContinuousZoom();
  46. var map_ac = new BMap.Autocomplete( //建立一个自动完成的对象
  47. {
  48. "input": "suggestId",
  49. "location": map
  50. });
  51. var marker = new BMap.Marker(center); // 创建标注
  52. map.addOverlay(marker); // 将标注添加到地图中
  53. marker.enableDragging(); //可拖拽
  54. //marker.setAnimation(BMAP_ANIMATION_BOUNCE);
  55. map_ac.addEventListener("onconfirm", function (e) { //鼠标点击下拉列表后的事件
  56. var _value = e.item.value;
  57. function myFun() {
  58. var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
  59. map.centerAndZoom(pp, 8);
  60. marker.setPosition(pp);
  61. mgetLocation(pp);
  62. }
  63. myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
  64. var local = new BMap.LocalSearch(map, { //智能搜索
  65. onSearchComplete: myFun
  66. });
  67. local.search(myValue);
  68. });
  69. var opts = {
  70. width: 200, // 信息窗口宽度
  71. height: 30, // 信息窗口高度
  72. title: "", // 信息窗口标题
  73. enableMessage: false,//设置允许信息窗发送短息
  74. };
  75. var infoWindow = new BMap.InfoWindow("拖动我设置你的位置", opts); // 创建信息窗口对象
  76. marker.openInfoWindow(infoWindow);
  77. var gc = new BMap.Geocoder();
  78. function msetpoint(e) {
  79. document.getElementById("lng_input").value = e.point.lng;
  80. document.getElementById("lat_input").value = e.point.lat;
  81. marker.setPosition(new BMap.Point(e.point.lng, e.point.lat));
  82. mgetLocation(e.point);
  83. }
  84. function mgetLocation(point) {
  85. document.getElementById("lng_input").value = point.lng;
  86. document.getElementById("lat_input").value = point.lat;
  87. gc.getLocation(point, function (rs) {
  88. var addComp = rs.addressComponents;
  89. marker.openInfoWindow(new BMap.InfoWindow(rs.address, opts));
  90. document.getElementById("address_input").value = rs.address;
  91. document.getElementById("city_input").value = addComp.city;
  92. document.getElementById("province_input").value = addComp.province;
  93. document.getElementById("district_input").value = addComp.district;
  94. //alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
  95. });
  96. }
  97. marker.addEventListener("dragend", msetpoint);
  98. map.addEventListener("click", msetpoint);
  99. function tilesloaded() {
  100. map.setCenter(center); //设置地图中心点。center除了可以为坐标点以外,还支持城市名
  101. mgetLocation(center);
  102. // map.setZoom(8); //将视图切换到指定的缩放等级,中心点坐标不变
  103. map.removeEventListener("tilesloaded", tilesloaded);
  104. }
  105. map.addEventListener("tilesloaded", tilesloaded);
  106. function confirm() {
  107. var data = {};
  108. data.lng = document.getElementById("lng_input").value;
  109. data.lat = document.getElementById("lat_input").value;
  110. data.address = document.getElementById("address_input").value;
  111. data.city = document.getElementById("city_input").value;
  112. data.province = document.getElementById("province_input").value;
  113. data.district = document.getElementById("district_input").value;
  114. return data;
  115. }
  116. </script>
  117. </body>
  118. </html>