|
|
@@ -0,0 +1,71 @@
|
|
|
+package com.mgtech.base_library.util;
|
|
|
+
|
|
|
+import android.location.Location;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ProjectName: Bua
|
|
|
+ * @Package: com.mgtech.base_library.util
|
|
|
+ * @ClassName: LocationUtil
|
|
|
+ * @Description: 位置工具类
|
|
|
+ * @Author: 牛松涛
|
|
|
+ * @CreateDate: 2020/7/17 17:04
|
|
|
+ * @UpdateUser: 更新者:Administrator
|
|
|
+ * @UpdateDate: 2020/7/17 17:04
|
|
|
+ * @UpdateRemark: 更新说明:
|
|
|
+ * @Version: 1.0
|
|
|
+ */
|
|
|
+public class LocationUtil {
|
|
|
+
|
|
|
+ private static LocationUtil instance;
|
|
|
+
|
|
|
+
|
|
|
+ public static LocationUtil getInstance(){
|
|
|
+ if(instance == null){
|
|
|
+ synchronized (LocationUtil.class){
|
|
|
+ if(instance == null){
|
|
|
+ instance = new LocationUtil();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取两个坐标之间距离
|
|
|
+ * @param startLat 起始坐标经度
|
|
|
+ * @param startLng 起始坐标纬度
|
|
|
+ * @param endLat 终点坐标经度
|
|
|
+ * @param endLng 终点坐标经度
|
|
|
+ * @return 两点之间距离
|
|
|
+ */
|
|
|
+ public float getDistance(double startLat,double startLng,double endLat,double endLng){
|
|
|
+ float[] results = new float[3];
|
|
|
+ Location.distanceBetween(startLat,startLng,endLat,endLng,results);
|
|
|
+ return results[0];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取两个坐标之间距离
|
|
|
+ * @param startLat 起始坐标经度
|
|
|
+ * @param startLng 起始坐标纬度
|
|
|
+ * @param endLat 终点坐标经度
|
|
|
+ * @param endLng 终点坐标经度
|
|
|
+ * @return 两点之间距离
|
|
|
+ */
|
|
|
+ DecimalFormat df;
|
|
|
+ public String getDistanceFormat(double startLat,double startLng,double endLat,double endLng){
|
|
|
+ if (df == null)
|
|
|
+ df = new DecimalFormat("#.0");
|
|
|
+ float[] results = new float[3];
|
|
|
+ Location.distanceBetween(startLat,startLng,endLat,endLng,results);
|
|
|
+ if (results[0] < 1000)
|
|
|
+ return df.format(results[0]) + "m";
|
|
|
+ else{
|
|
|
+ return df.format(BigDecimal.valueOf(results[0] / 1000)) + "km";
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|