index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. (function (root, factory) {
  2. if (typeof exports === 'object') {
  3. module.exports = factory();
  4. } else if (typeof define === 'function' && define.amd) {
  5. define(factory);
  6. } else {
  7. root.BMapLib = root.BMapLib || {};
  8. root.BMapLib.CurveLine = root.BMapLib.CurveLine || factory();
  9. }
  10. })(this, function() {
  11. function CurveLine(points, opts) {
  12. try {
  13. BMap;
  14. } catch (e) {
  15. throw Error('Baidu Map JS API is not ready yet!');
  16. }
  17. var self = this;
  18. var curvePoints = getCurvePoints(points);
  19. var polyline = new BMap.Polyline(curvePoints, opts);
  20. polyline.addEventListener('lineupdate', function(){
  21. if (this.isEditing) {
  22. this.enableEditing();
  23. }
  24. });
  25. polyline.cornerPoints = points;
  26. polyline.editMarkers = []; //缂栬緫鍔熻兘鐨勯《鐐�
  27. /**
  28. * 閲嶅啓寮х嚎鐨勭紪杈戝姛鑳�
  29. */
  30. polyline.enableEditing = function () {
  31. var self = this;
  32. if (self.map) {
  33. self.disableEditing();
  34. for (var i = 0; i < self.cornerPoints.length; i++) {
  35. var marker = new BMap.Marker(self.cornerPoints[i], {
  36. icon: new BMap.Icon('http://api.map.baidu.com/library/CurveLine/1.5/src/circle.png', new BMap.Size(16,16)),
  37. enableDragging: true,
  38. raiseOnDrag: true
  39. });
  40. marker.addEventListener('dragend', function(){
  41. self.cornerPoints.length = 0;
  42. for (var i = 0; i < self.editMarkers.length; i++) {
  43. self.cornerPoints.push(self.editMarkers[i].getPosition());
  44. }
  45. var curvePoints = getCurvePoints(self.cornerPoints)
  46. self.setPath(curvePoints);
  47. });
  48. marker.index = i;
  49. self.editMarkers.push(marker);
  50. self.map.addOverlay(marker);
  51. }
  52. }
  53. self.isEditing = true;
  54. }
  55. /**
  56. * 閲嶅啓寮х嚎鐨勭紪杈戝姛鑳�
  57. */
  58. polyline.disableEditing = function() {
  59. this.isEditing = false;
  60. //娓呯┖涔嬪墠鐨勭紪杈戠偣
  61. for (var i = 0; i < this.editMarkers.length; i++) {
  62. this.map.removeOverlay(this.editMarkers[i]);
  63. this.editMarkers[i] = null;
  64. }
  65. this.editMarkers.length = 0;
  66. }
  67. /**
  68. * 鑾峰彇寮х嚎鐨勫潗鏍囩偣
  69. */
  70. polyline.getPath = function() {
  71. return curvePoints;
  72. }
  73. // polyline.points = points; //寮х嚎鐨勫潗鏍囬《鐐�
  74. // polyline.enableEditing = enableEditing;
  75. // polyline.disableEditing = disableEditing;
  76. // polyline.getPath = getPath;
  77. return polyline;
  78. }
  79. function extend(child, parent) {
  80. for (var p in parent) {
  81. if (parent.hasOwnProperty(p)) {
  82. child[p] = parent[p];
  83. }
  84. }
  85. return child;
  86. };
  87. function getCurvePoints(points) {
  88. var curvePoints = [];
  89. for (var i = 0; i < points.length - 1; i++) {
  90. var p = getCurveByTwoPoints(points[i], points[i + 1]);
  91. if (p && p.length > 0) {
  92. curvePoints = curvePoints.concat(p);
  93. }
  94. }
  95. return curvePoints;
  96. }
  97. function getCurveByTwoPoints(obj1, obj2) {
  98. var curveCoordinates = [];
  99. if (!obj1 || !obj2 || !(obj1 instanceof BMap.Point) || !(obj2 instanceof BMap.Point)) {
  100. return null;
  101. }
  102. var B1 = function(x) {
  103. return 1 - 2 * x + x * x;
  104. };
  105. var B2 = function(x) {
  106. return 2 * x - 2 * x * x;
  107. };
  108. var B3 = function(x) {
  109. return x * x;
  110. };
  111. var count=30; // 鏇茬嚎鏄敱涓€浜涘皬鐨勭嚎娈电粍鎴愮殑锛岃繖涓〃绀鸿繖涓洸绾挎墍鏈夊埌鐨勬姌绾跨殑涓暟
  112. var isFuture=false;
  113. var t, h, h2, lat3, lng3, j, t2;
  114. var LnArray = [];
  115. var i = 0;
  116. var inc = 0;
  117. if (typeof(obj2) == "undefined") {
  118. if (typeof(curveCoordinates) != "undefined") {
  119. curveCoordinates = [];
  120. }
  121. return;
  122. }
  123. var lat1 = parseFloat(obj1.lat);
  124. var lat2 = parseFloat(obj2.lat);
  125. var lng1 = parseFloat(obj1.lng);
  126. var lng2 = parseFloat(obj2.lng);
  127. // 璁$畻鏇茬嚎瑙掑害鐨勬柟娉�
  128. if (lng2 > lng1) {
  129. if (parseFloat(lng2-lng1) > 180) {
  130. if (lng1 < 0) {
  131. lng1 = parseFloat(180 + 180 + lng1);
  132. }
  133. }
  134. }
  135. if (lng1 > lng2) {
  136. if (parseFloat(lng1-lng2) > 180) {
  137. if (lng2 < 0) {
  138. lng2 = parseFloat(180 + 180 + lng2);
  139. }
  140. }
  141. }
  142. j = 0;
  143. t2 = 0;
  144. if (lat2 == lat1) {
  145. t = 0;
  146. h = lng1 - lng2;
  147. } else if (lng2 == lng1) {
  148. t = Math.PI / 2;
  149. h = lat1 - lat2;
  150. } else {
  151. t = Math.atan((lat2 - lat1) / (lng2 - lng1));
  152. h = (lat2 - lat1) / Math.sin(t);
  153. }
  154. if (t2 == 0) {
  155. t2 = (t + (Math.PI / 5));
  156. }
  157. h2 = h / 2;
  158. lng3 = h2 * Math.cos(t2) + lng1;
  159. lat3 = h2 * Math.sin(t2) + lat1;
  160. for (i = 0; i < count + 1; i++) {
  161. curveCoordinates.push(new BMap.Point(
  162. (lng1 * B1(inc) + lng3 * B2(inc)) + lng2 * B3(inc),
  163. (lat1 * B1(inc) + lat3 * B2(inc) + lat2 * B3(inc))
  164. ));
  165. inc = inc + (1 / count);
  166. }
  167. return curveCoordinates;
  168. }
  169. return CurveLine;
  170. });