功能說明:需要拿到經緯度來繪製地圖軌跡。解決思路是先把經緯度保存到一個文件中,然後從文件中讀取經緯度數據,然後在地圖上面繪製。
一、讀取文件和保存文件的工具類
public class FileStoreTool {
private static final String TAG = "FileStoreTool";
private static ArrayList<String> newList;
private Context mContext;
public FileStoreTool(Context context){
this.mContext=context;
}
/**
*
* 保存文件的位置
*
*/
public String getSDPath() {
boolean hasSDCard = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
if (hasSDCard) {
return Environment.getExternalStorageDirectory().toString();
} else {
return Environment.getDownloadCacheDirectory().toString();
}
}
/**
*
* 保存內容到文件
*
*/
public boolean saveFile(String str, String filePath) {
boolean isSaved=false;
FileOutputStream fos = null;
try {
File file = new File(filePath);
if (!file.exists()){
file.createNewFile();
}
fos = new FileOutputStream(file,true);
str+="\r\n"; //換行符
fos.write(str.getBytes());
fos.flush();
isSaved=true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fos)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return isSaved;
}
/**
* 讀文件裏的內容
* @param filePath
* @return
*/
public String readSDFile(String filePath) {
StringBuffer sb = new StringBuffer();
File file = new File(filePath);
try {
FileInputStream fis = new FileInputStream(file);
int c;
while ((c = fis.read()) != -1) {
Log.d(TAG,"c == "+c);
sb.append((char) c);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 按行讀取內容
* @param strFilePath
* @return
*/
public static ArrayList<String> ReadTxtFile(String strFilePath)
{
String path = strFilePath;
newList=new ArrayList<String>();
//打開文件
File file = new File(path);
//如果path是傳遞過來的參數,可以做一個非目錄的判斷
if (file.isDirectory())
{
Log.d("TestFile", "The File doesn't not exist.");
}
else
{
try {
InputStream instream = new FileInputStream(file);
if (instream != null)
{
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
//分行讀取
while (( line = buffreader.readLine()) != null) {
Log.d(TAG,"line == "+line);
newList.add(line+"\n");
}
instream.close();
}
}
catch (java.io.FileNotFoundException e)
{
Log.d("TestFile", "The File doesn't not exist.");
}
catch (IOException e)
{
Log.d("TestFile", e.getMessage());
}
}
return newList;
}
}
二、獲取了經緯度數據並且保存
//創建一個實體類
public class Longitude {
private String strInfo;
public String getStrInfo() {
return strInfo;
}
public void setStrInfo(String strInfo) {
this.strInfo = strInfo;
}
}
//經緯度信息
private String strInfo ="";
//經緯度改變的回調方法
@Override
public void onLocationChanged(QxLocation qxLocation) {
Log.i(TAG, "onLocationChanged: " + qxLocation.getAddress() + " 緯度: " + qxLocation.getLatitude() + " 經度: " + qxLocation.getLongitude());
strInfo=qxLocation.getLatitude()+","+qxLocation.getLongitude();
Longitude mLongitude = new Longitude();
mLongitude.setStrInfo(strInfo);
fileStoreTool.saveFile(mLongitude.getStrInfo(), "文件路徑"+ File.separator+FILE_NAME);
}
三、添加一個按鈕,點擊後讀取經緯度的數據
public void onGetLocationData(View view) {
ArrayList<String> infos=fileStoreTool.ReadTxtFile("/storage/emulated/0"+File.separator+FILE_NAME);
StringBuffer sb = new StringBuffer();
for(int i = 0; i < infos.size(); i++){
sb.append(infos.get(i));
}
textview.setText("經緯度信息:"+sb.toString());
}
`四、下面是實現從文件中去經緯度的數據,然後把數據添加到地圖上面顯示,邏輯處理在查詢軌跡TrackQueryActivity.java中處理,具體代碼可以從百度地圖官網下載SDK運行處理。
public static final String FILE_NAME = "save.txt"; //保存的文件名
private void initListener() {
mTrackListener = new OnTrackListener() {
@Override
public void onHistoryTrackCallback(HistoryTrackResponse response) {
int total = response.getTotal();
StringBuffer sb = new StringBuffer(256);
// if (StatusCodes.SUCCESS != response.getStatus()) {
// viewUtil.showToast(TrackQueryActivity.this, response.getMessage());
// } else if (0 == total) {
// viewUtil.showToast(TrackQueryActivity.this, getString(R.string.no_track_data));
// } else {
List<TrackPoint> points = response.getTrackPoints();
if (null != points) {
for (TrackPoint trackPoint : points) {
// Log.d(TAG,"維度:"+trackPoint.getLocation().getLatitude()+"經度:"+trackPoint.getLocation().getLongitude());
if (!CommonUtil.isZeroPoint(trackPoint.getLocation().getLatitude(),
trackPoint.getLocation().getLongitude())) {
//trackPoints.add(MapUtil.convertTrace2Map(trackPoint.getLocation()));
}
}
}
//這裏是讀取文件裏面存儲的經緯度,然後在把經緯度添加到地圖上面顯示
ArrayList<String> infos = ReadTxtFile("/storage/emulated/0"+File.separator+FILE_NAME);
StringBuffer sb1 = new StringBuffer();
for(int i = 0; i < infos.size(); i++){
Log.e("wq892373445","經緯度:"+infos.get(i));
String aa = infos.get(i);
String mLatitude = aa.substring(0, aa.indexOf(","));
Log.d("wq892373445","維度:"+mLatitude);
String mLongitude = aa.substring(mLatitude.length()+1, aa.length());
Log.d("wq892373445","經度:"+mLongitude);
LatLng mLatLng = new LatLng(Double.parseDouble(mLatitude),Double.parseDouble(mLongitude));
if (!CommonUtil.isZeroPoint(Double.parseDouble(mLatitude),
Double.parseDouble(mLongitude))) {
trackPoints.add(mLatLng);
}
sb1.append(infos.get(i));
}
// sb.append("總里程:");
// sb.append(response.getDistance());
// sb.append("米");
// sb.append("\n收費里程:");
// sb.append(response.getTollDistance());
// sb.append("米");
// sb.append("\n低速里程:");
// sb.append(response.getLowSpeedDistance());
// sb.append("米");
// //addView(mapUtil.mapView);
// mHistoryTrackView.setText(sb.toString());
//}
if (total > Constants.PAGE_SIZE * pageIndex) {
historyTrackRequest.setPageIndex(++pageIndex);
queryHistoryTrack();
} else {
mapUtil.drawHistoryTrack(trackPoints, sortType);
}
}
@Override
public void onDistanceCallback(DistanceResponse response) {
super.onDistanceCallback(response);
}
@Override
public void onLatestPointCallback(LatestPointResponse response) {
super.onLatestPointCallback(response);
}
};
經過以上的流程來處理雖然是在百度上面顯示了軌跡,但是會看到百度座標系有偏移,經過找很多資料,百度官方提供資料如下:
通用座標轉換方法,支持其他座標轉BD09
// 將google地圖、soso地圖、aliyun地圖、mapabc地圖和amap地圖// 所用座標轉換成百度座標
CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordType.COMMON);
// sourceLatLng待轉換座標
converter.coord(sourceLatLng);
LatLng desLatLng = converter.convert();
// 將GPS設備採集的原始GPS座標轉換成百度座標
CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordType.GPS);
// sourceLatLng待轉換座標
converter.coord(sourceLatLng);
LatLng desLatLng = converter.convert();
自動座標轉換,支持GCJ02座標輸入/輸出
自Android v4.3起,一次聲明GCJ02座標類型,全應用自動執行座標轉換,即輸入GCJ02座標,返回GCJ02座標。
聲明座標類型的代碼如下:
SDKInitializer.setCoordType(CoordType.GCJ02);//默認爲BD09LL座標
也可以獲取當前使用的座標類型:
SDKInitializer.getCoordType();//BD09LL或者GCJ02座標
注意事項
1. 自動座標轉換方法僅適用於國內(包括港澳臺地區)且輸入座標爲GCJ02座標的情況。
2. 百度地圖國外即使用WGS84座標,如需要支持海外地區,直接使用WGS84座標訪問即可,無需轉換。如需要同時訪問國內和國外數據,自動座標轉換方法不適用。
百度官網地址:https://lbsyun.baidu.com/index.php?title=androidsdk/guide/coordtrans
最後我的解決方法如下:
解決思路:1、採用自動座標轉換沒有作用。2、就是把GPS的原始數據轉爲百度座標(這種方法有用)
LatLng mLatLng = new LatLng(Double.parseDouble(mLatitude),Double.parseDouble(mLongitude));
// 將GPS設備採集的原始GPS座標轉換成百度座標
CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordinateConverter.CoordType.GPS);
// sourceLatLng待轉換座標
converter.coord(mLatLng);
LatLng desLatLng = converter.convert();
if (!CommonUtil.isZeroPoint(Double.parseDouble(mLatitude),
Double.parseDouble(mLongitude))) {
trackPoints.add(desLatLng);
}