| 1 | package livegps;
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.Color;
|
|---|
| 4 | import java.awt.Graphics;
|
|---|
| 5 | import java.awt.Point;
|
|---|
| 6 | import java.beans.PropertyChangeEvent;
|
|---|
| 7 | import java.beans.PropertyChangeListener;
|
|---|
| 8 | import java.util.ArrayList;
|
|---|
| 9 | import java.util.Collection;
|
|---|
| 10 | import java.util.Date;
|
|---|
| 11 |
|
|---|
| 12 | import org.openstreetmap.josm.Main;
|
|---|
| 13 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 14 | import org.openstreetmap.josm.gui.MapView;
|
|---|
| 15 | import org.openstreetmap.josm.gui.layer.RawGpsLayer;
|
|---|
| 16 |
|
|---|
| 17 | public class LiveGpsLayer extends RawGpsLayer implements PropertyChangeListener {
|
|---|
| 18 | public final static String LAYER_NAME = "LiveGPS layer";
|
|---|
| 19 | LatLon lastPos;
|
|---|
| 20 | GpsPoint lastPoint;
|
|---|
| 21 | Collection<GpsPoint> trackBeingWritten;
|
|---|
| 22 | float speed;
|
|---|
| 23 | float course;
|
|---|
| 24 | String status;
|
|---|
| 25 | //JLabel lbl;
|
|---|
| 26 | boolean autocenter;
|
|---|
| 27 |
|
|---|
| 28 | public LiveGpsLayer(Collection<Collection<GpsPoint>> data)
|
|---|
| 29 | {
|
|---|
| 30 | super (data, LAYER_NAME, null);
|
|---|
| 31 | if (data.isEmpty())
|
|---|
| 32 | {
|
|---|
| 33 | data.add(new ArrayList<GpsPoint>());
|
|---|
| 34 | }
|
|---|
| 35 | // use last track in collection:
|
|---|
| 36 | for (Collection<GpsPoint> track : data) {
|
|---|
| 37 | trackBeingWritten = track;
|
|---|
| 38 | }
|
|---|
| 39 | //lbl = new JLabel();
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | void setCurrentPosition(double lat, double lon)
|
|---|
| 43 | {
|
|---|
| 44 | LatLon thisPos = new LatLon(lat, lon);
|
|---|
| 45 | if ((lastPos != null) && (thisPos.equalsEpsilon(lastPos))) {
|
|---|
| 46 | // no change in position
|
|---|
| 47 | // maybe show a "paused" cursor or some such
|
|---|
| 48 | return;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | lastPos = thisPos;
|
|---|
| 52 | lastPoint = new GpsPoint (thisPos, new Date().toString());
|
|---|
| 53 | // synchronize when adding data, as otherwise the autosave action
|
|---|
| 54 | // needs concurrent access and this results in an exception!
|
|---|
| 55 | synchronized (LiveGpsLock.class) {
|
|---|
| 56 | trackBeingWritten.add(lastPoint);
|
|---|
| 57 | }
|
|---|
| 58 | if (autocenter) {
|
|---|
| 59 | center();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | //Main.map.repaint();
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | public void center()
|
|---|
| 66 | {
|
|---|
| 67 | if (lastPoint != null)
|
|---|
| 68 | Main.map.mapView.zoomTo(lastPoint.eastNorth, Main.map.mapView.getScale());
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | // void setStatus(String status)
|
|---|
| 72 | // {
|
|---|
| 73 | // this.status = status;
|
|---|
| 74 | // Main.map.repaint();
|
|---|
| 75 | // System.out.println("LiveGps status: " + status);
|
|---|
| 76 | // }
|
|---|
| 77 |
|
|---|
| 78 | void setSpeed(float metresPerSecond)
|
|---|
| 79 | {
|
|---|
| 80 | speed = metresPerSecond;
|
|---|
| 81 | //Main.map.repaint();
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | void setCourse(float degrees)
|
|---|
| 85 | {
|
|---|
| 86 | course = degrees;
|
|---|
| 87 | //Main.map.repaint();
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void setAutoCenter(boolean ac)
|
|---|
| 91 | {
|
|---|
| 92 | autocenter = ac;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | @Override public void paint(Graphics g, MapView mv)
|
|---|
| 96 | {
|
|---|
| 97 | super.paint(g, mv);
|
|---|
| 98 | // int statusHeight = 50;
|
|---|
| 99 | // Rectangle mvs = mv.getBounds();
|
|---|
| 100 | // mvs.y = mvs.y + mvs.height - statusHeight;
|
|---|
| 101 | // mvs.height = statusHeight;
|
|---|
| 102 | // g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.8f));
|
|---|
| 103 | // g.fillRect(mvs.x, mvs.y, mvs.width, mvs.height);
|
|---|
| 104 |
|
|---|
| 105 | if (lastPoint != null)
|
|---|
| 106 | {
|
|---|
| 107 | Point screen = mv.getPoint(lastPoint.eastNorth);
|
|---|
| 108 | g.setColor(Color.RED);
|
|---|
| 109 | g.drawOval(screen.x-10, screen.y-10,20,20);
|
|---|
| 110 | g.drawOval(screen.x-9, screen.y-9,18,18);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | // lbl.setText("gpsd: "+status+" Speed: " + speed + " Course: "+course);
|
|---|
| 114 | // lbl.setBounds(0, 0, mvs.width-10, mvs.height-10);
|
|---|
| 115 | // Graphics sub = g.create(mvs.x+5, mvs.y+5, mvs.width-10, mvs.height-10);
|
|---|
| 116 | // lbl.paint(sub);
|
|---|
| 117 |
|
|---|
| 118 | // if(status != null) {
|
|---|
| 119 | // g.setColor(Color.WHITE);
|
|---|
| 120 | // g.drawString("gpsd: " + status, 5, mv.getBounds().height - 15); // lower left corner
|
|---|
| 121 | // }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /* (non-Javadoc)
|
|---|
| 125 | * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
|
|---|
| 126 | */
|
|---|
| 127 | public void propertyChange(PropertyChangeEvent evt) {
|
|---|
| 128 | if(!visible) {
|
|---|
| 129 | return;
|
|---|
| 130 | }
|
|---|
| 131 | if("gpsdata".equals(evt.getPropertyName())) {
|
|---|
| 132 | LiveGpsData data = (LiveGpsData) evt.getNewValue();
|
|---|
| 133 | if(data.isFix()) {
|
|---|
| 134 | setCurrentPosition(data.getLatitude(), data.getLongitude());
|
|---|
| 135 | if(!Float.isNaN(data.getSpeed())) {
|
|---|
| 136 | setSpeed(data.getSpeed());
|
|---|
| 137 | }
|
|---|
| 138 | if(!Float.isNaN(data.getCourse())) {
|
|---|
| 139 | setCourse(data.getCourse());
|
|---|
| 140 | }
|
|---|
| 141 | Main.map.repaint();
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | }
|
|---|