map.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. jQuery.noConflict();
  2. (function (window, angular, $) {
  3. var mp = new BMap.Map("allmap", {minZoom: 10, maxZoom: 20});
  4. // 初始化地图及级别(天府广场)
  5. mp.centerAndZoom(new BMap.Point(104.072363, 30.663316), 18);
  6. // 开启鼠标滚轮缩放
  7. mp.enableScrollWheelZoom(true);
  8. // 圆形覆盖物
  9. function CircleOverlay(point, text, mouseoverText) {
  10. this._point = point;
  11. this._text = text;
  12. this._overText = mouseoverText;
  13. }
  14. CircleOverlay.prototype = new BMap.Overlay();
  15. CircleOverlay.prototype.initialize = function (map) {
  16. this._map = map;
  17. var div = this._div = document.createElement("div");
  18. div.style.position = "absolute";
  19. div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
  20. div.style.backgroundColor = "#00a75b";
  21. div.style.border = "1px solid #00a75b";
  22. div.style.width = '100px';
  23. div.style.height = '100px';
  24. div.style['border-radius'] = '50%';
  25. div.style.color = "white";
  26. div.style['text-align'] = 'center';
  27. div.style.fontSize = "12px";
  28. var span = this._span = document.createElement("span");
  29. span.style.position = 'absolute';
  30. span.style.width = '100%';
  31. span.style.top = '40px';
  32. span.style.left = '0px';
  33. //span.style.left='20px';
  34. span.style['white-spance'] = 'nowrap';
  35. div.appendChild(span);
  36. span.appendChild(document.createTextNode(this._text));
  37. var that = this;
  38. mp.getPanes().labelPane.appendChild(div);
  39. return div;
  40. };
  41. CircleOverlay.prototype.draw = function () {
  42. var map = this._map;
  43. var pixel = map.pointToOverlayPixel(this._point);
  44. // this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
  45. this._div.style.top = pixel.y - 30 + "px";
  46. };
  47. // 复杂的自定义覆盖物
  48. function ComplexCustomOverlay(point, text, mouseoverText) {
  49. this._point = point;
  50. this._text = text;
  51. this._overText = mouseoverText;
  52. }
  53. ComplexCustomOverlay.prototype = new BMap.Overlay();
  54. ComplexCustomOverlay.prototype.initialize = function (map) {
  55. this._map = map;
  56. var div = this._div = document.createElement("div");
  57. div.style.position = "absolute";
  58. div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
  59. div.style.backgroundColor = "#EE5D5B";
  60. div.style.border = "1px solid #BC3B3A";
  61. div.style.color = "white";
  62. div.style.height = "18px";
  63. div.style.padding = "0 2px";
  64. div.style.lineHeight = "18px";
  65. div.style.whiteSpace = "nowrap";
  66. div.style.MozUserSelect = "none";
  67. div.style.fontSize = "12px";
  68. var span = this._span = document.createElement("span");
  69. div.appendChild(span);
  70. span.appendChild(document.createTextNode(this._text));
  71. var that = this;
  72. var arrow = this._arrow = document.createElement("div");
  73. // arrow.style.background = "url(//map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label.png) no-repeat";
  74. arrow.style.position = "absolute";
  75. arrow.style.width = "11px";
  76. arrow.style.height = "10px";
  77. arrow.style.top = "22px";
  78. arrow.style.left = "10px";
  79. arrow.style.overflow = "hidden";
  80. div.appendChild(arrow);
  81. div.onmouseover = function () {
  82. this.style.backgroundColor = "#6BADCA";
  83. this.style.borderColor = "#0000ff";
  84. this.getElementsByTagName("span")[0].innerHTML = that._overText;
  85. arrow.style.backgroundPosition = "0px -20px";
  86. };
  87. div.onmouseout = function () {
  88. this.style.backgroundColor = "#EE5D5B";
  89. this.style.borderColor = "#BC3B3A";
  90. this.getElementsByTagName("span")[0].innerHTML = that._text;
  91. arrow.style.backgroundPosition = "0px 0px";
  92. };
  93. mp.getPanes().labelPane.appendChild(div);
  94. return div;
  95. };
  96. ComplexCustomOverlay.prototype.draw = function () {
  97. var map = this._map;
  98. var pixel = map.pointToOverlayPixel(this._point);
  99. this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
  100. this._div.style.top = pixel.y - 30 + "px";
  101. };
  102. //获取模块
  103. var app = angular.module("app.map", [
  104. 'eccrm.angular',
  105. 'app.mobile'
  106. ]);
  107. app.controller('Ctrl', function ($scope, AlertService, CommunityImportService) {
  108. $scope.condition = {};
  109. var marker = null;
  110. // 根据输入的小区或地址名称进行定位
  111. $scope.search = function () {
  112. var myGeo = new BMap.Geocoder();
  113. myGeo.getPoint($scope.name, function (point) {
  114. if (point) {
  115. mp.panTo(point);
  116. mp.setZoom(18);
  117. if (marker == null) {
  118. marker = new BMap.Marker(point);
  119. mp.addOverlay(marker);
  120. marker.setAnimation(BMAP_ANIMATION_BOUNCE);
  121. marker.disableMassClear();
  122. } else {
  123. marker.setPosition(point);
  124. }
  125. $scope.query();
  126. } else {
  127. AlertService.info("您填写的地址没有解析到结果!");
  128. }
  129. }, "成都市");
  130. };
  131. // 获取地图的可视范围
  132. mp.addEventListener('moveend', function (e) {
  133. $scope.query();
  134. });
  135. mp.addEventListener('zoomend', function (e) {
  136. $scope.query();
  137. });
  138. var initArea = false;
  139. $scope.query = function () {
  140. // 获取地图的大小
  141. var zoomLevel = mp.getZoom();
  142. if (zoomLevel <= 16) {
  143. // 如果小于15,则显示区域价格
  144. if (!initArea) {
  145. $scope.queryAreaPrice();
  146. initArea = true;
  147. }
  148. } else {
  149. // 否则显示房屋价格
  150. $scope.queryCommunityPrice();
  151. }
  152. };
  153. $scope.queryAreaPrice = function () {
  154. // 清除地图上所有的覆盖物
  155. mp.clearOverlays();
  156. CommunityImportService.queryAreaPrice(function (o) {
  157. o = o.data || [];
  158. var myGeo = new BMap.Geocoder();
  159. angular.forEach(o, function (house) {
  160. myGeo.getPoint(house.areaName, function (point) {
  161. if (point) {
  162. var txt = house.areaName + " " + (parseInt(house.price));
  163. var myCompOverlay = new CircleOverlay(point, txt, txt + "(" + house.total || 0 + "套)");
  164. mp.addOverlay(myCompOverlay);
  165. }
  166. }, "成都市");
  167. });
  168. });
  169. };
  170. var houseMap = {};
  171. $scope.queryCommunityPrice = function () {
  172. var bs = mp.getBounds(); //获取可视区域
  173. var bssw = bs.getSouthWest(); //可视区域左下角
  174. var bsne = bs.getNorthEast(); //可视区域右上角
  175. var condition = $scope.condition;
  176. // 判断位置发生了变化没有。如果发生了变化,则重新加载数据
  177. if (condition.longitudeGE === bssw.lng && condition.longitudeLE === bsne.lng
  178. && condition.latitudeGE === bssw.lat && condition.latitudeLE === bsne.lat) {
  179. return;
  180. }
  181. condition.longitudeGE = bssw.lng;
  182. condition.longitudeLE = bsne.lng;
  183. condition.latitudeGE = bssw.lat;
  184. condition.latitudeLE = bsne.lat;
  185. // 清除地图上所有的覆盖物
  186. // mp.clearOverlays();
  187. // 加载新的数据
  188. CommunityImportService.queryHousePrices(condition, function (o) {
  189. o = o.data || [];
  190. angular.forEach(o, function (house) {
  191. if (!house.longitude) {
  192. return;
  193. }
  194. if (houseMap[house.houses]) {
  195. return;
  196. }
  197. var txt = house.houses + " " + (house.price);
  198. var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(house.longitude, house.latitude), txt, txt);
  199. mp.addOverlay(myCompOverlay);
  200. houseMap[house.houses] = myCompOverlay;
  201. });
  202. });
  203. };
  204. $scope.queryCommunityPrice();
  205. });
  206. })(window, angular, Zepto);