📄 track.java
字号:
Float speed = Float.readFloat(dataIn);
maxRead++;
long stamp = dataIn.readLong();
System.out.println("[" + i + "]" + lon + " " + lat + " alt:" + alt + " dir:" + dir + " sp:" + speed + " " + stamp);
coordSeg[2 * i + 1] = MapCanvas.xFromLon(lon);
coordSeg[2 * i] = MapCanvas.yFromLat(lat);
times[i] = stamp;
alts[i] = (int)alt.toLong();
courses[i] = (int)dir.toLong();
speeds[i] = (int)speed.toLong();
}
res = coordSeg;
for(int i = 0; i < nbWaypoints; i++)
{
int size = dataIn.read();
byte descr[] = new byte[size];
dataIn.read(descr);
String descrS = new String(descr);
System.out.println(descrS);
OneLoc l = OneLoc.createFromString(descrS);
listPlaces.addElement(l);
}
}
}
catch(Exception e)
{
System.out.print("Error, while reading at: maxRead:" + maxRead + " sizeTrack:" + sizeTrack);
e.printStackTrace();
}
} else
{
res = coordSeg;
}
return res;
}
public Vector getWaypoints()
{
loadTrack();
return listPlaces;
}
protected void sendKML(OutputStream os)
{
PrintStream ps = new PrintStream(os);
ps.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
ps.print("<kml xmlns=\"http://earth.google.com/kml/2.0\">\n");
ps.print("<Folder>\n");
if(getWaypoints() != null)
{
for(Enumeration e = getWaypoints().elements(); e.hasMoreElements(); ps.print(" </Placemark>\n"))
{
OneLoc l = (OneLoc)e.nextElement();
ps.print(" <Placemark>\n");
if(l.name != null)
ps.print(" <name>" + l.name + "</name>\n");
if(l.description != null)
ps.print(" <description>" + l.description + "</description>\n");
ps.print(" <Style>\n");
ps.print(" <IconStyle><color>ff" + Integer.toHexString(l.m_col) + "</color></IconStyle>\n");
ps.print(" </Style>\n");
ps.print(" <Point>\n");
ps.print(" <coordinates>" + l.m_lon + "," + l.m_lat + "," + "0.0" + "</coordinates>\n");
ps.print(" </Point>\n");
}
}
if(getTrack() != null)
{
ps.print(" <Placemark>\n");
ps.print("<styleUrl>root://styles#default+icon=0x307</styleUrl>");
ps.print(" <LineString>\n");
ps.print(" <coordinates>\n ");
for(int i = 0; i < coordSeg.length; i += 2);
ps.print("\n </coordinates>\n");
ps.print(" </LineString>\n");
ps.print(" </Placemark>\n");
}
ps.print("</Folder>\n");
ps.print("</kml>");
ps.print("");
}
protected void sendGPX(OutputStream os)
{
PrintStream ps = new PrintStream(os);
ps.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
ps.print("<gpx version=\"1.0\" creator=\"J2MEMAP http://j2memap.landspurg.net\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n \t\txmlns=\"http://www.topografix.com/GPX/1/0\"\n \t\txsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
if(getWaypoints() != null)
{
for(Enumeration e = getWaypoints().elements(); e.hasMoreElements(); ps.print(" </wpt>\n"))
{
OneLoc l = (OneLoc)e.nextElement();
ps.print(" <wpt lat=\"" + l.m_lat + "\" lon=\"" + l.m_lon + "\">\n");
if(l.name != null)
ps.print(" <name>" + l.name + "</name>\n");
if(l.description != null)
ps.print(" <desc>" + l.description + "</desc>\n");
}
}
if(getTrack() != null)
{
ps.print(" <trk>\n <trkseg>\n");
for(int i = 0; i < coordSeg.length; i += 2)
{
Float lat = MapCanvas.convLat(coordSeg[i]);
Float lon = MapCanvas.convLon(coordSeg[i + 1]);
ps.print(" <trkpt lat=\"" + lat + "\" lon=\"" + lon + "\">\n");
if(alts != null)
ps.print(" <ele>" + alts[i / 2] + "</ele>\n");
if(speeds != null)
ps.print(" <speed>" + speeds[i / 2] + "</speed>\n");
if(courses != null)
ps.print(" <course>" + courses[i / 2] + "</course>\n");
if(this.times != null)
{
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(this.times[i / 2]));
String times = cal.get(1) + "-" + frmt(cal.get(2) + 1, 2) + "-" + frmt(cal.get(5), 2) + "T" + frmt(cal.get(11), 2) + ":" + frmt(cal.get(12), 2) + ":" + frmt(cal.get(13), 2) + "Z";
ps.print(" <time>" + times + "</time>\n");
}
ps.print(" </trkpt>\n");
}
ps.print(" </trkseg>\n </trk>\n");
}
ps.print("</gpx>\n");
ps.print("");
}
private String frmt(int inVal, int inSize)
{
String res;
for(res = inVal + ""; res.length() < inSize; res = "0" + res);
if(res.length() > inSize)
res = res.substring(res.length() - inSize);
return res;
}
public void saveTrack()
{
checkIfStreamIsInit();
try
{
if(savedTrack == null)
{
sizeTrack = (bo.size() - 88) / 28;
nbWaypoints = listPlaces.size();
for(int i = 0; i < nbWaypoints; i++)
{
byte byteSt[] = ((OneLoc)listPlaces.elementAt(i)).saveToString().getBytes();
dataOut.writeInt(byteSt.length);
bo.write(byteSt);
}
savedTrack = bo.toByteArray();
bo.reset();
writeLong(savedTrack, 64, 4, sizeTrack);
writeLong(savedTrack, 68, 4, nbWaypoints);
}
RecordStore rs = RecordStore.openRecordStore("GPSTrack", true);
byte name[] = m_name.getBytes("UTF-8");
if(name.length != 0)
System.arraycopy(name, 0, savedTrack, 0, name.length);
savedTrack[name.length] = 0;
savedTrack[name.length + 1] = 0;
if(m_rmsId == -1)
m_rmsId = rs.addRecord(savedTrack, 0, savedTrack.length);
else
rs.setRecord(m_rmsId, savedTrack, 0, savedTrack.length);
rs.closeRecordStore();
loadHeader();
if(m_tn_glob != null)
m_tn_glob.onTrackSaved(this);
}
catch(Exception e)
{
UtilMidp.showException(e);
}
}
public void addLoc(OneLoc newLoc)
{
listPlaces.addElement(newLoc);
nbWaypoints++;
}
void checkIfStreamIsInit()
{
try
{
if(bo == null)
{
bo = new ByteArrayOutputStream();
dataOut = new DataOutputStream(bo);
}
if(bo.size() == 0)
{
byte nameS[] = (new String("newTrack")).getBytes();
byte name[] = new byte[64 - nameS.length];
dataOut.write(nameS);
dataOut.write(name);
dataOut.writeInt(0);
dataOut.writeInt(0);
long time = System.currentTimeMillis();
System.out.println("TimeStamp:" + time + " " + (new Date(time)).toString());
dataOut.writeLong(time);
dataOut.writeLong(0L);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void addRecord(oneGPSPos newLoc)
{
try
{
if(newLoc.stamp - lastStamp > (long)(m_curRate * 1000))
{
checkIfStreamIsInit();
dataOut.writeLong(newLoc.stamp);
currRecord++;
System.out.println("one record added....Total:" + currRecord);
lastStamp = newLoc.stamp;
}
}
catch(Exception e)
{
e.printStackTrace();
}
if(currRecord >= nb_recordsMax && currRecord == nb_recordsMax)
saveTrack();
}
public void delete()
{
if(m_rmsId != -1)
try
{
RecordStore rs = RecordStore.openRecordStore("GPSTrack", true);
rs.deleteRecord(m_rmsId);
m_rmsId = -1;
rs.closeRecordStore();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setName(String inName)
{
if(m_rmsId != -1)
{
loadTrack();
m_name = inName;
saveTrack();
}
m_name = inName;
}
public int getSizeTrack()
{
return sizeTrack;
}
public int getNbWayPoints()
{
return nbWaypoints;
}
public long getTimeStamp()
{
return timeStamp;
}
public static void removeTrack(Track t)
{
tracks.removeElement(t);
t.delete();
}
public void refreshTrack(MapCanvas m_map)
{
setUrl(m_url, m_map);
}
public void setTrackListener(TrackListener inListener)
{
m_trackListener = inListener;
}
public void fireSelected(OneLoc theLoc)
{
if(m_trackListener != null)
m_trackListener.onLocSelected(this, theLoc);
}
public boolean fireOpened(OneLoc theLoc)
{
if(m_trackListener != null)
return m_trackListener.onLocOpened(this, theLoc);
else
return false;
}
Display m_display;
Alert m_a;
String m_url;
String m_realUrl;
public boolean isInit;
int m_rmsId;
public int coordSeg[];
public long times[];
public int alts[];
public int courses[];
public int speeds[];
public static OneLoc m_defaultLocType = new OneLoc(new Float(0L), new Float(0L));
public OneLoc m_locType;
public String m_name;
public Vector listPlaces;
public byte savedTrack[];
static TrackNotifier m_tn_glob = null;
TrackNotifier m_tn;
int nbWaypoints;
int sizeTrack;
boolean loaded;
int modeTrack;
long timeStamp;
public boolean m_autoDisplay;
public static final int sizeElem = 28;
public static final int sizeHeader = 88;
static ByteArrayOutputStream bo = null;
static DataOutputStream dataOut = null;
public static int currRecord = 0;
static int nb_recordsMax = 1000;
public static int m_curRate = 5;
static long lastStamp = -1L;
public static Vector tracks = new Vector();
public static final int TYPE_EXPORT_KML = 0;
public static final int TYPE_EXPORT_GPX = 1;
public static final int REFRESH_MODE_NONE = 0;
public static final int REFRESH_MODE_ON_CHANGE = 1;
public static final int REFRESH_MODE_ON_INTERVAL = 2;
public int m_refreshType;
private TrackListener m_trackListener;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -