地图标注 坐标转换 找不到so INVALID_USER_KEY INVALID_USER_SCODE
由于全球定位系统(GPS)的周数翻转事件(GPS Week Number Rollover),部分GPS芯片会因未对“GPS周数翻转”进行兼容处理,导致从GPS中获取的日期持续错误。在GPS周数翻转时刻,高德开放平台将采用系统时间和gps对比的方法,如果二者相差超过一年,则时分秒取gps时间,年月日取系统时间,以解决周数翻转带来的时间跳变问题使开发者内部用错时间。具体可参考以下代码:
package com.amap.api.location;
import java.util.Calendar;
import java.util.Date;
public class DateUtil{
private static final long DAY_IN_MILLIS = 24 * 60 * 60 * 1000;
/**
* 入口
* 校正gps时间
* @param gpsTime 原始的gps时间
* @return
*/public static long correctGpsTime(long gpsTime){
if(Math.abs(gpsTime - System.currentTimeMillis()) > 365 * 24 * 60 * 60 * 1000) {
return getNewGpsTime(gpsTime);
} else {
return gpsTime;
}
}
/**
* 获取新的GPS时间
* @param gpsTime 原始的gps时间
* @return
*/private static long getNewGpsTime(long gpsTime){
//系统时间当前时间戳long sysTime = System.currentTimeMillis();
//从GPS时间戳中获取当前0点到现在的毫秒数long millisSecondOfDayInGps = getMillisSecondOfDay(gpsTime);
//newTime = 系统时间到当天零点毫秒数 + gps 0点到现在的毫秒数long newTime = getDayInMillis(sysTime) + millisSecondOfDayInGps;
//系统时间与计算出的gps时间差值long absTime = Math.abs(newTime - sysTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(newTime));
int hour = calendar.get(Calendar.HOUR_OF_DAY);
//差值阈值, 23小时long diffMillisThreshold = 23 * 60 * 60 * 1000;
//解决跨0点问题, 0点收到昨天23点的数据if (hour == 23 && absTime >= diffMillisThreshold) {
//减去一天时间
newTime -= DAY_IN_MILLIS;
}
//解决跨0点问题, 23点收到明天0点的数据if (hour == 0 && absTime >= diffMillisThreshold) {
//加上一天时间
newTime += DAY_IN_MILLIS;
}
return newTime;
}
/**
* 从时间戳中获取当天0点到现在的毫秒数(只取时分秒)
* @param time
* @return
*/private static long getMillisSecondOfDay(long time){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(time));
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long newTime = calendar.getTimeInMillis();
return (time - newTime);
}
/**
* 从时间戳中获取从1970.1.1号到当天0点的毫秒数(只取年月日)
* @param time
* @return
*/private static long getDayInMillis(long time){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(time));
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long newTime = calendar.getTimeInMillis();
return newTime;
}
}