经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android实现百度地图两点画弧线
来源:jb51  时间:2019/1/14 9:17:37  对本文有异议

本文实例为大家分享了Android实现百度地图两点画弧线的具体代码,供大家参考,具体内容如下

  1. import android.support.annotation.NonNull;
  2.  
  3. import com.baidu.mapapi.map.ArcOptions;
  4. import com.baidu.mapapi.map.OverlayOptions;
  5. import com.baidu.mapapi.model.LatLng;
  6.  
  7. /**
  8. *
  9. * <a>http://lbsyun.baidu.com/index.php?title=androidsdk/guide/render-map/ploygon</a>
  10. * 通过两点来绘制弧线
  11. * @author peter 2018-12-24 15:09
  12. */
  13. public class ArcOverlay {
  14. private LatLng start;
  15. private LatLng end;
  16. /**
  17. * {@link com.baidu.mapapi.map.ArcOptions#color(int)}
  18. */
  19. private int color;//弧线的颜色
  20. private int arcWidth = 4;//弧线宽度
  21.  
  22. public ArcOverlay(@NonNull LatLng start, @NonNull LatLng end, int color) {
  23. this.start = start;
  24. this.end = end;
  25. this.color = color;
  26. }
  27.  
  28. /**
  29. * 获取一个弧线Overlay
  30. * @param start 起点
  31. * @param end 终点
  32. * @param color 颜色
  33. * @param arcWidth 弧线宽度
  34. */
  35. public ArcOverlay(@NonNull LatLng start, @NonNull LatLng end, int color, int arcWidth) {
  36. this.start = start;
  37. this.end = end;
  38. this.color = color;
  39. this.arcWidth = arcWidth;
  40. }
  41.  
  42. public OverlayOptions toBmapOverlayOptions() {
  43. return new ArcOptions()
  44. .color(color)
  45. .width(arcWidth)
  46. .points(start, getMidPoint(), end);
  47. }
  48.  
  49. /**
  50. * 参考前端百度提供的画弧线js文件中计算第三个点的方式
  51. * <a>http://lbsyun.baidu.com/jsdemo.htm#c1_13</a>
  52. * <a>view-source:http://api.map.baidu.com/library/CurveLine/1.5/src/CurveLine.min.js<a/>
  53. * @return 中间点的经纬度
  54. */
  55. private LatLng getMidPoint() {
  56. double t, t2, h,h2;
  57. double lng1 = start.longitude;
  58. double lng2 = end.longitude;
  59. double lat1 = start.latitude;
  60. double lat2 = end.latitude;
  61.  
  62. if (lng2 > lng1) {
  63. if ((lng2 - lng1) > 180) {
  64. if (lng1 < 0) {
  65. lng1 = (180 + 180 + lng1);
  66. }
  67. }
  68. }
  69. if (lng1 > lng2) {
  70. if ((lng1 - lng2) > 180) {
  71. if (lng2 < 0) {
  72. lng2 = (180 + 180 + lng2);
  73. }
  74. }
  75. }
  76. if (lat2 == lat1) {
  77. t = 0;
  78. h = lng1 - lng2;
  79. } else {
  80. if (lng2 == lng1) {
  81. t = Math.PI / 2;
  82. h = lat1 - lat2;
  83. } else {
  84. t = Math.atan((lat2 - lat1) / (lng2 - lng1));
  85. h = (lat2 - lat1) / Math.sin(t);
  86. }
  87. }
  88. t2 = (t + (Math.PI / 5));
  89. h2 = h / 2;
  90. double lng3 = h2 * Math.cos(t2) + lng1;
  91. double lat3 = h2 * Math.sin(t2) + lat1;
  92. return new LatLng(lat3,lng3);
  93. }
  94.  
  95.  
  96. public LatLng getStart() {
  97. return start;
  98. }
  99.  
  100. public void setStart(LatLng start) {
  101. this.start = start;
  102. }
  103.  
  104. public LatLng getEnd() {
  105. return end;
  106. }
  107.  
  108. public void setEnd(LatLng end) {
  109. this.end = end;
  110. }
  111.  
  112. public int getColor() {
  113. return color;
  114. }
  115.  
  116. public void setColor(int color) {
  117. this.color = color;
  118. }
  119.  
  120. public int getArcWidth() {
  121. return arcWidth;
  122. }
  123.  
  124. public void setArcWidth(int arcWidth) {
  125. this.arcWidth = arcWidth;
  126. }
  127. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号