/** * */ package com.reactnativecomponent.amap; import android.graphics.Color; import android.text.TextUtils; import com.amap.api.location.AMapLocation; import java.text.SimpleDateFormat; import java.util.Locale; import android.content.Context; public class Utils { /** * 开始定位 */ public final static int MSG_LOCATION_START = 0; /** * 定位完成 */ public final static int MSG_LOCATION_FINISH = 1; /** * 停止定位 */ public final static int MSG_LOCATION_STOP= 2; public final static String KEY_URL = "URL"; public final static String URL_H5LOCATION = "file:///android_asset/location.html"; /** * 根据定位结果返回定位信息的字符串 * @param * @return */ public synchronized static String getLocationStr(AMapLocation location){ if(null == location){ return null; } StringBuffer sb = new StringBuffer(); //errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明 if(location.getErrorCode() == 0){ sb.append("定位成功" + "\n"); sb.append("定位类型: " + location.getLocationType() + "\n"); sb.append("经 度 : " + location.getLongitude() + "\n"); sb.append("纬 度 : " + location.getLatitude() + "\n"); sb.append("精 度 : " + location.getAccuracy() + "米" + "\n"); sb.append("提供者 : " + location.getProvider() + "\n"); if (location.getProvider().equalsIgnoreCase( android.location.LocationManager.GPS_PROVIDER)) { // 以下信息只有提供者是GPS时才会有 sb.append("速 度 : " + location.getSpeed() + "米/秒" + "\n"); sb.append("角 度 : " + location.getBearing() + "\n"); // 获取当前提供定位服务的卫星个数 sb.append("星 数 : " + location.getSatellites() + "\n"); } else { // 提供者是GPS时是没有以下信息的 sb.append("国 家 : " + location.getCountry() + "\n"); sb.append("省 : " + location.getProvince() + "\n"); sb.append("市 : " + location.getCity() + "\n"); sb.append("城市编码 : " + location.getCityCode() + "\n"); sb.append("区 : " + location.getDistrict() + "\n"); sb.append("区域 码 : " + location.getAdCode() + "\n"); sb.append("地 址 : " + location.getAddress() + "\n"); sb.append("兴趣点 : " + location.getPoiName() + "\n"); //定位完成的时间 sb.append("定位时间: " + formatUTC(location.getTime(), "yyyy-MM-dd HH:mm:ss") + "\n"); } } else { //定位失败 sb.append("定位失败" + "\n"); sb.append("错误码:" + location.getErrorCode() + "\n"); sb.append("错误信息:" + location.getErrorInfo() + "\n"); sb.append("错误描述:" + location.getLocationDetail() + "\n"); } //定位之后的回调时间 sb.append("回调时间: " + formatUTC(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "\n"); return sb.toString(); } private static SimpleDateFormat sdf = null; public synchronized static String formatUTC(long l, String strPattern) { if (TextUtils.isEmpty(strPattern)) { strPattern = "yyyy-MM-dd HH:mm:ss"; } if (sdf == null) { try { sdf = new SimpleDateFormat(strPattern, Locale.CHINA); } catch (Throwable e) { } } else { sdf.applyPattern(strPattern); } return sdf == null ? "NULL" : sdf.format(l); } /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } public static int parseColor(String color){ char[] chars = color.split("#")[1].toCharArray(); String red="",green="",blue="",alpha=""; switch(color.length()){ case 4: //#rgb red = chars[0]+""+chars[0]; green = chars[1]+""+chars[1]; blue = chars[2]+""+chars[2]; alpha = ""; break; case 5: //#rgba red = chars[0]+""+chars[0]; green = chars[1]+""+chars[1]; blue = chars[2]+""+chars[2]; alpha = chars[3]+""+chars[3]; break; case 7: //#rrggbb red = chars[0]+""+chars[1]; green = chars[2]+""+chars[3]; blue = chars[4]+""+chars[5]; alpha = ""; break; case 9: //#rrggbbaa red = chars[0]+""+chars[1]; green = chars[2]+""+chars[3]; blue = chars[4]+""+chars[5]; alpha = chars[6]+""+chars[7]; break; } return Color.parseColor("#"+alpha+red+green+blue); } }