marker-direction.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * LEAFLET.MARKER_DRECTION
  3. * vesion 0.0.1
  4. * @author Thomas Zou
  5. * @date 10/1/2018
  6. */
  7. /* Angle ICON */
  8. L.AngleIcon = L.Icon.extend({
  9. // default value
  10. options: {
  11. angle: 0,
  12. iconSize: new L.Point(32,32), // canvas size
  13. className: "leaflet-boat-icon",
  14. course: 0, // angle
  15. labelAnchor: [0, -0], // test loaction
  16. pointType:0, // pointType
  17. ciFlag :false,
  18. label:'',
  19. textColor:'red',
  20. img:null
  21. },
  22. createIcon: function () {
  23. var e = document.createElement("canvas");
  24. this._setIconStyles(e, "icon");
  25. var s = this.options.iconSize;
  26. e.width = s.x;
  27. e.height = s.y;
  28. this.ctx = e.getContext("2d");
  29. this.draw( s.x, s.y);
  30. return e;
  31. },
  32. draw: function( w, h) {
  33. if(!this.ctx)
  34. return;
  35. var course = this.options.course;
  36. /*this.ctx.font="10px Courier New";
  37. this.ctx.fillStyle = this.options.textColor;
  38. if(this.options.ciFlag){
  39. var text = this.options.label;
  40. this.ctx.fillText(text, 0, 20);
  41. }*/
  42. var img = this._createImg(this.options.iconUrl,img);
  43. var _this = this;
  44. img.onload = function(){
  45. //平移坐标原点
  46. _this.ctx.translate(w/2,h/2);
  47. //旋转画布
  48. _this.ctx.rotate(_this.options.course);
  49. _this.ctx.translate(-w/2,-h/2);
  50. //画图
  51. _this.ctx.drawImage(img,0,0,w,h);
  52. //console.log(img.src)
  53. }
  54. },
  55. /**
  56. * 获得两点之间方向角 (Get the direction angle between two points)
  57. * 根据地球上两点之间的经纬度计算两点之间与正北方向的夹角
  58. * (According to the latitude and longitude between two points on earth,
  59. * calculate the angle between two points and the direction of the north)
  60. *
  61. * @param 点A(L.Latlng) PointA
  62. * @param 点B(L.Latlng) PointB
  63. * @return result: AB角度 (the angle of AB (calculated result degree))
  64. *
  65. * @version 2016-08-19
  66. *
  67. * @see Math.atan2()用于返回从x轴到指定坐标点(x, y)的角度(以弧度为单位);y/x = tan#
  68. * Math.atan2 () returns the angle (in radians) from the x-axis to the specified coordinate point (x, y); y / x = tan #
  69. * @version 2016-08-19
  70. */
  71. getAngle : function(A, B){
  72. var angle = null;
  73. var latA = A.lat;
  74. var lonA = A.lon;
  75. var latB = B.lat;
  76. var lonB = B.lon;
  77. // 注意经度或者纬度相等 (when longitude or latitude is equal)
  78. if(lonA == lonB && latA>latB ){
  79. angle = Math.PI;
  80. }
  81. else if(lonA == lonB && latA < latB ){
  82. angle = 0 ;
  83. }
  84. else if(lonA > lonB && latA == latB ){
  85. angle = -(Math.PI/2);
  86. }
  87. else if(lonA < lonB && latA == latB ){
  88. angle = Math.PI/2 ;
  89. }
  90. // 注意经度或者纬度都不相等 (Longitude and latitude are not equal)
  91. else{
  92. var x1 = A.lat*Math.pow(10,12);
  93. var x2 = B.lat*Math.pow(10,12);
  94. var y1 = A.lon*Math.pow(10,12);
  95. var y2 = B.lon*Math.pow(10,12);
  96. angle = Math.atan2(y2-y1,x2-x1)
  97. }
  98. options.angle = angle;
  99. return angle;
  100. },
  101. /**
  102. * 设置maker 和正北方向 角度
  103. * Set the angle between the maker and the north
  104. * @param heading (Unit: radian)
  105. */
  106. setHeading : function(heading) {
  107. if(!heading){
  108. heading = options.angle;
  109. }
  110. this.options.course = (heading % 360);
  111. var s = this.options.iconSize;
  112. //我们不需要在这里显示的调用draw()方法,因为map.addLayer(layer)会调用这个方法
  113. //We do not need to call draw() as shown here
  114. //because map.addLayer (layer) will call this method
  115. //this.draw(this.ctx, s.x, s.y);
  116. }
  117. });
  118. // AngleMarker继承Marker并添加setHeading 和 getAngle 方法
  119. // AngleMarker extends from Marker and adds the setHeading and getAngle methods
  120. L.AngleMarker = L.Marker.extend({
  121. getAngle : function (A, B) {
  122. return this.options.icon.getAngle(A, B);
  123. },
  124. setHeading: function(heading) {
  125. this.options.icon.setHeading(heading);
  126. }
  127. });
  128. L.angleMarker = function(pos, options) {
  129. options.icon = new L.AngleIcon({
  130. ciFlag:options.labelFlag,
  131. label:options.label,
  132. textColor:options.labelColor,
  133. img: options.img,
  134. course:options.course,
  135. iconUrl:options.iconUrl
  136. });
  137. return new L.AngleMarker(pos,options);
  138. };