Utils.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. *
  3. */
  4. package com.reactnativecomponent.amap;
  5. import android.graphics.Color;
  6. import android.text.TextUtils;
  7. import com.amap.api.location.AMapLocation;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Locale;
  10. import android.content.Context;
  11. public class Utils {
  12. /**
  13. * 开始定位
  14. */
  15. public final static int MSG_LOCATION_START = 0;
  16. /**
  17. * 定位完成
  18. */
  19. public final static int MSG_LOCATION_FINISH = 1;
  20. /**
  21. * 停止定位
  22. */
  23. public final static int MSG_LOCATION_STOP= 2;
  24. public final static String KEY_URL = "URL";
  25. public final static String URL_H5LOCATION = "file:///android_asset/location.html";
  26. /**
  27. * 根据定位结果返回定位信息的字符串
  28. * @param
  29. * @return
  30. */
  31. public synchronized static String getLocationStr(AMapLocation location){
  32. if(null == location){
  33. return null;
  34. }
  35. StringBuffer sb = new StringBuffer();
  36. //errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
  37. if(location.getErrorCode() == 0){
  38. sb.append("定位成功" + "\n");
  39. sb.append("定位类型: " + location.getLocationType() + "\n");
  40. sb.append("经 度 : " + location.getLongitude() + "\n");
  41. sb.append("纬 度 : " + location.getLatitude() + "\n");
  42. sb.append("精 度 : " + location.getAccuracy() + "米" + "\n");
  43. sb.append("提供者 : " + location.getProvider() + "\n");
  44. if (location.getProvider().equalsIgnoreCase(
  45. android.location.LocationManager.GPS_PROVIDER)) {
  46. // 以下信息只有提供者是GPS时才会有
  47. sb.append("速 度 : " + location.getSpeed() + "米/秒" + "\n");
  48. sb.append("角 度 : " + location.getBearing() + "\n");
  49. // 获取当前提供定位服务的卫星个数
  50. sb.append("星 数 : "
  51. + location.getSatellites() + "\n");
  52. } else {
  53. // 提供者是GPS时是没有以下信息的
  54. sb.append("国 家 : " + location.getCountry() + "\n");
  55. sb.append("省 : " + location.getProvince() + "\n");
  56. sb.append("市 : " + location.getCity() + "\n");
  57. sb.append("城市编码 : " + location.getCityCode() + "\n");
  58. sb.append("区 : " + location.getDistrict() + "\n");
  59. sb.append("区域 码 : " + location.getAdCode() + "\n");
  60. sb.append("地 址 : " + location.getAddress() + "\n");
  61. sb.append("兴趣点 : " + location.getPoiName() + "\n");
  62. //定位完成的时间
  63. sb.append("定位时间: " + formatUTC(location.getTime(), "yyyy-MM-dd HH:mm:ss") + "\n");
  64. }
  65. } else {
  66. //定位失败
  67. sb.append("定位失败" + "\n");
  68. sb.append("错误码:" + location.getErrorCode() + "\n");
  69. sb.append("错误信息:" + location.getErrorInfo() + "\n");
  70. sb.append("错误描述:" + location.getLocationDetail() + "\n");
  71. }
  72. //定位之后的回调时间
  73. sb.append("回调时间: " + formatUTC(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "\n");
  74. return sb.toString();
  75. }
  76. private static SimpleDateFormat sdf = null;
  77. public synchronized static String formatUTC(long l, String strPattern) {
  78. if (TextUtils.isEmpty(strPattern)) {
  79. strPattern = "yyyy-MM-dd HH:mm:ss";
  80. }
  81. if (sdf == null) {
  82. try {
  83. sdf = new SimpleDateFormat(strPattern, Locale.CHINA);
  84. } catch (Throwable e) {
  85. }
  86. } else {
  87. sdf.applyPattern(strPattern);
  88. }
  89. return sdf == null ? "NULL" : sdf.format(l);
  90. }
  91. /**
  92. * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
  93. */
  94. public static int dip2px(Context context, float dpValue) {
  95. final float scale = context.getResources().getDisplayMetrics().density;
  96. return (int) (dpValue * scale + 0.5f);
  97. }
  98. public static int parseColor(String color){
  99. char[] chars = color.split("#")[1].toCharArray();
  100. String red="",green="",blue="",alpha="";
  101. switch(color.length()){
  102. case 4: //#rgb
  103. red = chars[0]+""+chars[0];
  104. green = chars[1]+""+chars[1];
  105. blue = chars[2]+""+chars[2];
  106. alpha = "";
  107. break;
  108. case 5: //#rgba
  109. red = chars[0]+""+chars[0];
  110. green = chars[1]+""+chars[1];
  111. blue = chars[2]+""+chars[2];
  112. alpha = chars[3]+""+chars[3];
  113. break;
  114. case 7: //#rrggbb
  115. red = chars[0]+""+chars[1];
  116. green = chars[2]+""+chars[3];
  117. blue = chars[4]+""+chars[5];
  118. alpha = "";
  119. break;
  120. case 9: //#rrggbbaa
  121. red = chars[0]+""+chars[1];
  122. green = chars[2]+""+chars[3];
  123. blue = chars[4]+""+chars[5];
  124. alpha = chars[6]+""+chars[7];
  125. break;
  126. }
  127. return Color.parseColor("#"+alpha+red+green+blue);
  128. }
  129. }