Map.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Namespace.register("U.Map"); /* 12软件林志雄*/
  2. /*
  3. 需要的变量 :城市名 详细地址 地图级别 地图的宽高 父亲节点
  4. */
  5. U.Map.defaults = { //默认初始化
  6. "city": "深圳市", "width": "1000", "height": "500", "em": "body", "add": "深圳市罗湖区人民北路金溪大厦"
  7. }
  8. U.Map.defaults.init = function (obj) { //传进来的json值进行覆盖
  9. for (var i in obj) { U.Map.defaults[i] = obj[i]; }
  10. return U.Map.defaults;
  11. }
  12. U.Map.Qs = function (info, _L, _T) {//起始 @parm add 详细地址 @parm city 填城市名 @parm width 元素的宽 @parm height 元素的高 @parm em 父级
  13. info = U.Map.defaults.init(info);
  14. //U.Map.ceateEle(info); //创建元素
  15. var Total = info.em;
  16. var mapTotal = $$("div", { "id": "U_M_Total", style: { "position": "relative", "width": info.width + "px"} }, Total); //地图创建
  17. var result = $$("div", { "id": "map-result", style: { "margin-top": 10 + "px", "position": "absolute", "top": 10 + "px", "right": 10 + "px", "z-index": "99"} }, mapTotal); //搜索框创建
  18. $$("input", { "id": "suggestId", "type": "text", style: { "width": 260 + "px", "padding-top": 9 + "px", "padding-bottom": 9 + "px", "padding-left": 10 + "px", "font-size": 14 + "px", "color": "#333" }, "placeholder": "请输入地址" }, result);
  19. $$("div", { "id": "searchBtn", style: { "width": 43 + "px", "height": "38" + "px", "float": "right", "background": "url('http://d.1473.cn/controls/pc/Map/searchicon.png')", "cursor": "pointer"} }, result);
  20. $$("div", { "id": "l-map", style: { "height": info.height + "px", "width": info.width + "px"} }, mapTotal); //地图创建
  21. //var ss = U.Map.defaults();
  22. //var s = U.Map.defaults.apply(U.Map.defaults, ([""]));
  23. cityinfo = info.add;
  24. city = "深圳市";
  25. var map = new BMap.Map("l-map");
  26. var myGeo = new BMap.Geocoder();
  27. var top_left_control = new BMap.ScaleControl({ anchor: BMAP_ANCHOR_TOP_LEFT }); // 左上角,添加比例尺
  28. var top_left_navigation = new BMap.NavigationControl(); //左上角,添加默认缩放平移控件
  29. map.addControl(top_left_control);
  30. map.addControl(top_left_navigation);
  31. myGeo.getPoint(cityinfo, function (point) { // 将地址解析结果显示在地图上,并调整地图视野
  32. if (point) {
  33. map.centerAndZoom(point, 20); // 初始化地图,设置城市和地图级别。
  34. map.addOverlay(new BMap.Marker(point));
  35. info.em[0].contentWindow.document.body.appendChild(mapTotal);
  36. $(mapTotal).addClass("UCD_IF_CT"); U.CD.L.AppendIF(mapTotal, info.em, _L, _T);
  37. // document.body.appendChild($("#tangram-suggestion--TANGRAM__1x-main")[0])
  38. sss();
  39. } else {
  40. alert("您选择地址没有解析到结果!");
  41. }
  42. }, city);
  43. // map.centerAndZoom("深圳", 20);
  44. function sss() {
  45. var top_left_control = new BMap.ScaleControl({ anchor: BMAP_ANCHOR_TOP_LEFT }); // 左上角,添加比例尺
  46. var top_left_navigation = new BMap.NavigationControl(); //左上角,添加默认缩放平移控件
  47. map.addControl(top_left_control);
  48. map.addControl(top_left_navigation);
  49. var ac = new BMap.Autocomplete( //建立一个自动完成的对象
  50. {"input": "suggestId"
  51. , "location": map
  52. });
  53. ac.addEventListener("onhighlight", function (e) { //鼠标放在下拉列表上的事件
  54. var str = "";
  55. var _value = e.fromitem.value;
  56. var value = "";
  57. if (e.fromitem.index > -1) {
  58. value = _value.province + _value.city + _value.district + _value.street + _value.business;
  59. }
  60. str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
  61. value = "";
  62. if (e.toitem.index > -1) {
  63. _value = e.toitem.value;
  64. value = _value.province + _value.city + _value.district + _value.street + _value.business;
  65. }
  66. str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
  67. $("#searchResultPanel").innerHTML = str;
  68. });
  69. var myValue;
  70. ac.addEventListener("onconfirm", function (e) { //鼠标点击下拉列表后的事件
  71. var _value = e.item.value;
  72. myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
  73. $("#searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
  74. U.Map.setPlace();
  75. });
  76. U.Map.setPlace = function () {//搜索函数
  77. map.clearOverlays(); //清除地图上所有覆盖物
  78. U.Map.fun = function () {
  79. var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
  80. map.centerAndZoom(pp, 20);
  81. map.addOverlay(new BMap.Marker(pp)); //添加标注
  82. }
  83. var local = new BMap.LocalSearch(map, { //智能搜索
  84. onSearchComplete: U.Map.fun
  85. });
  86. local.search(myValue);
  87. }
  88. }
  89. }
  90. U.Map.ceateEle = function (info) { //创建地图元素 @param
  91. //var Total = $("#" + info.em)[0];
  92. var Total = info.em;
  93. var mapTotal = $$("div", { "id": "U_M_Total", style: { "position": "relative", "width": info.width + "px"} }, Total); //地图创建
  94. var result = $$("div", { "id": "map-result", style: { "margin-top": 10 + "px", "position": "absolute", "top": 10 + "px", "right": 10 + "px", "z-index": "99"} }, mapTotal); //搜索框创建
  95. $$("input", { "id": "suggestId", "type": "text", style: { "width": 260 + "px", "padding-top": 9 + "px", "padding-bottom": 9 + "px", "padding-left": 10 + "px", "font-size": 14 + "px", "color": "#333" }, "placeholder": "请输入地址" }, result);
  96. $$("div", { "id": "searchBtn", style: { "width": 43 + "px", "height": "38" + "px", "float": "right", "background": "url('searchicon.png')", "cursor": "pointer"} }, result);
  97. $$("div", { "id": "l-map", style: { "height": info.height + "px", "width": info.width + "px"} }, Total); //地图创建
  98. }
  99. U.Map.drapMap = function (obj) {
  100. return U.Map.Qs(obj);
  101. }