欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android实现百度地图两点画弧线

时间:2021-05-21 08:24:45|栏目:Android代码|点击:

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

import android.support.annotation.NonNull;

import com.baidu.mapapi.map.ArcOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;

/**
 *
 * http://lbsyun.baidu.com/index.php?title=androidsdk/guide/render-map/ploygon
 * 通过两点来绘制弧线
 * @author peter 2018-12-24 15:09
 */
public class ArcOverlay {
 private LatLng start;
 private LatLng end;
 /**
 * {@link com.baidu.mapapi.map.ArcOptions#color(int)}
 */
 private int color;//弧线的颜色
 private int arcWidth = 4;//弧线宽度

 public ArcOverlay(@NonNull LatLng start, @NonNull LatLng end, int color) {
 this.start = start;
 this.end = end;
 this.color = color;
 }

 /**
 * 获取一个弧线Overlay
 * @param start 起点
 * @param end 终点
 * @param color 颜色
 * @param arcWidth 弧线宽度
 */
 public ArcOverlay(@NonNull LatLng start, @NonNull LatLng end, int color, int arcWidth) {
 this.start = start;
 this.end = end;
 this.color = color;
 this.arcWidth = arcWidth;
 }

 public OverlayOptions toBmapOverlayOptions() {
 return new ArcOptions()
  .color(color)
  .width(arcWidth)
  .points(start, getMidPoint(), end);
 }

 /**
 * 参考前端百度提供的画弧线js文件中计算第三个点的方式
 * <a>http://lbsyun.baidu.com/jsdemo.htm#c1_13</a>
 * <a>view-source:http://api.map.baidu.com/library/CurveLine/1.5/src/CurveLine.min.js<a/>
 * @return 中间点的经纬度
 */
 private LatLng getMidPoint() {
 double t, t2, h,h2;
 double lng1 = start.longitude;
 double lng2 = end.longitude;
 double lat1 = start.latitude;
 double lat2 = end.latitude;

 if (lng2 > lng1) {
  if ((lng2 - lng1) > 180) {
  if (lng1 < 0) {
   lng1 = (180 + 180 + lng1);
  }
  }
 }
 if (lng1 > lng2) {
  if ((lng1 - lng2) > 180) {
  if (lng2 < 0) {
   lng2 = (180 + 180 + lng2);
  }
  }
 }
 if (lat2 == lat1) {
  t = 0;
  h = lng1 - lng2;
 } else {
  if (lng2 == lng1) {
  t = Math.PI / 2;
  h = lat1 - lat2;
  } else {
  t = Math.atan((lat2 - lat1) / (lng2 - lng1));
  h = (lat2 - lat1) / Math.sin(t);
  }
 }
 t2 = (t + (Math.PI / 5));
 h2 = h / 2;
 double lng3 = h2 * Math.cos(t2) + lng1;
 double lat3 = h2 * Math.sin(t2) + lat1;
 return new LatLng(lat3,lng3);
 }


 public LatLng getStart() {
 return start;
 }

 public void setStart(LatLng start) {
 this.start = start;
 }

 public LatLng getEnd() {
 return end;
 }

 public void setEnd(LatLng end) {
 this.end = end;
 }

 public int getColor() {
 return color;
 }

 public void setColor(int color) {
 this.color = color;
 }

 public int getArcWidth() {
 return arcWidth;
 }

 public void setArcWidth(int arcWidth) {
 this.arcWidth = arcWidth;
 }
}

上一篇:Android自定义控件之自定义组合控件(三)

栏    目:Android代码

下一篇:Android仿微信/支付宝密码输入框

本文标题:Android实现百度地图两点画弧线

本文地址:http://www.codeinn.net/misctech/125601.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有