功能说明:需要拿到经纬度来绘制地图轨迹。解决思路是先把经纬度保存到一个文件中,然后从文件中读取经纬度数据,然后在地图上面绘制。
一、读取文件和保存文件的工具类
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);
}