⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 track.java

📁 GPS Track connects to a GPS and records the path that you travel. Tracks can be uploaded to a web s
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// J2ME GPS Track
// Copyright (C) 2006 Dana Peters
// http://www.qcontinuum.org/gpstrack

package org.qcontinuum.gpstrack;

import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import henson.midp.Float;
import java.util.*;
import org.qcontinuum.astro.*;

public class Track implements Runnable {
    
    final static private int DEGREESTOINT = 46603;
    
    final static public int FORMAT_NONE = 0;
    final static public int FORMAT_BINARY = 1;
    final static public int FORMAT_KML = 2;
    final static public int FORMAT_GPX = 3;
    final static public int FORMAT_CSV = 4;

    final static public int DESTINATION_EMAIL = 1;
    final static public int DESTINATION_BLUETOOTH = 2;
    final static public int DESTINATION_FILE = 3;
    
    private final static String mFormatExtension[] = { "", ".bin", ".kml", ".gpx", ".csv" };
    private final static String mFormatName[] = { "", "bin", "kml", "gpx", "csv" };

    private RecordStore mRecordStore;
    private String mTrackName;
    private boolean mStopThread;
    private Float mMinDistance, mDistance;
    private long mMinTime;
    private int mNoFixTime, mRunTime, mStopTime;

    public Track() {
        mRecordStore = null;
        mTrackName = "";
    }
    
    public void open() throws RecordStoreException {
        Preferences preferences = GpsTrack.getPreferences();
        mMinDistance = new Float(preferences.getMinDistance());
        mDistance = new Float(0);
        mMinTime = preferences.getMinTime() * 1000;
        mNoFixTime = mRunTime = mStopTime = 0;
        mRecordStore = null;
        try {
            mTrackName = GpsTrack.getDateString(System.currentTimeMillis());
            mRecordStore = RecordStore.openRecordStore(mTrackName, true);
            mStopThread = false;
            new Thread(this).start();
        } catch (RecordStoreException ex) {
            if (mRecordStore != null)
                mRecordStore.closeRecordStore();
            throw ex;
        }
    }
    
    public void close() {
        mStopThread = true;
        if (mRecordStore != null) {
            try {
                mRecordStore.closeRecordStore();
            }
            catch (Exception ex) { }
            mRecordStore = null;
        }
    }

    public boolean isOpen() {
        return mRecordStore != null;
    }

    public int getNumRecords() {
        int numRecords = 0;
        if (mRecordStore != null) {
            try {
                numRecords = mRecordStore.getNumRecords();
            }
            catch (Exception ex) { }
        }
        return numRecords;
    }

    public String getName() {
        return mTrackName;
    }
    
    public Float getDistance() {
        return mDistance;
    }
    
    public int getNoFixTime() {
        return mNoFixTime;
    }
    
    public int getStopTime() {
        return mStopTime;
    }
    
    public int getRunTime() {
        return mRunTime;
    }

    public void run() {
        Thread thread = Thread.currentThread();
        EarthPosition earthPosition, lastEarthPosition = null, lastPointEarthPosition = null;
        long currentTime, deltaTime, lastTime = 0, lastPointTime = 0;
        Float distance;
        Gps gps = GpsTrack.getGps();
        try {
            while (!mStopThread) {
                thread.sleep(2000);
                currentTime = System.currentTimeMillis();
                if (lastTime == 0)
                    lastTime = currentTime;
                deltaTime = currentTime - lastTime;
                lastTime = currentTime;
                if (!gps.getFix()) {
                    mNoFixTime += deltaTime;
                } else {
                    if (gps.getSpeed().Less(3))
                        mStopTime += deltaTime;
                    else
                        mRunTime += deltaTime;
                    earthPosition = gps.getEarthPosition();
                    if (earthPosition != null) {
                        if (lastEarthPosition == null)
                            lastEarthPosition = earthPosition;
                        distance = lastEarthPosition.getDistance(earthPosition);
                        lastEarthPosition = earthPosition;
                        if (distance.Less(100))
                            mDistance = mDistance.Add(distance);
                        if (currentTime - lastPointTime >= mMinTime) {
                            if (lastPointEarthPosition == null)
                                lastPointEarthPosition = earthPosition;
                            distance = lastPointEarthPosition.getDistance(earthPosition);
                            if (distance.Less(10000) && distance.Great(mMinDistance)) {
                                writeRecord(earthPosition);
                                lastPointTime = currentTime;
                                lastPointEarthPosition = earthPosition;
                            }
                        }
                    }
                }
            }
        } catch (InterruptedException e) { }
    }

    private void writeRecord(EarthPosition earthPosition) {
        try {
            byte[] bytes = new byte[6];
            int latitude = (int)earthPosition.getLatitude().Mul(DEGREESTOINT).toLong();
            bytes[0] = (byte)(0xff & (latitude >> 16));
            bytes[1] = (byte)(0xff & (latitude >> 8));
            bytes[2] = (byte)(0xff & latitude);
            int longitude = (int)earthPosition.getLongitude().Mul(DEGREESTOINT).toLong();
            bytes[3] = (byte)(0xff & (longitude >> 16));
            bytes[4] = (byte)(0xff & (longitude >> 8));
            bytes[5] = (byte)(0xff & longitude);
            if (mRecordStore != null)
                mRecordStore.addRecord(bytes, 0, bytes.length);
        }
        catch (RecordStoreException e) { }
    }

    public static String getFormatExtension(int format) {
        return mFormatExtension[format];
    }

    public static String getFormatName(int format) {
        return mFormatName[format];
    }

    public static void write(int format, DataOutputStream out, String trackName, Gauge gauge) throws IOException, RecordStoreException {
        switch (format) {
            case FORMAT_BINARY:
                writeBinary(out, trackName, gauge);
                break;
            case FORMAT_KML:
                writeKml(out, trackName, gauge);
                break;
            case FORMAT_GPX:
                writeGpx(out, trackName, gauge);
                break;
            case FORMAT_CSV:
                writeCsv(out, trackName, gauge);
                break;
        }
    }
    
    private static void writeKml(DataOutputStream out, String trackName, Gauge gauge) throws IOException, RecordStoreException {
        RecordStore recordStore = null;
        try { 
            out.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n".getBytes());
            out.write("<kml xmlns=\"http://www.google.com/earth/kml/2\">\r\n".getBytes());
            out.write("  <Document>\r\n".getBytes());
            out.write("    <name>2006-11-01b</name>\r\n".getBytes());
            out.write("    <Style id=\"waypoint\">\r\n".getBytes());
            out.write("      <IconStyle>\r\n".getBytes());
            out.write("        <Icon>\r\n".getBytes());
            out.write("          <href>root://icons/bitmap-4.png</href>\r\n".getBytes());
            out.write("          <x>160</x>\r\n".getBytes());
            out.write("          <w>32</w>\r\n".getBytes());
            out.write("          <h>32</h>\r\n".getBytes());
            out.write("        </Icon>\r\n".getBytes());
            out.write("      </IconStyle>\r\n".getBytes());
            out.write("    </Style>\r\n".getBytes());
            out.write("    <Style id=\"track\">\r\n".getBytes());
            out.write("      <LineStyle>\r\n".getBytes());
            out.write("        <color>64eeee17</color>\r\n".getBytes());
            out.write("        <width>6</width>\r\n".getBytes());
            out.write("      </LineStyle>\r\n".getBytes());
            out.write("    </Style>\r\n".getBytes());
            recordStore = RecordStore.openRecordStore(trackName, false);
            int numRecords = recordStore.getNumRecords();
            byte[] bytes = new byte[6];
            for (int recordId = 1; recordId <= numRecords; recordId++) {
                recordStore.getRecord(recordId, bytes, 0);
                int latitude = 0x00ff0000 & (bytes[0] << 16);
                latitude |= 0x0000ff00 & (bytes[1] << 8);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -