1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/*
* Copyright © 2013 Mike Beattie <mike@ethernal.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
package org.altusmetrum.AltosDroid;
import java.util.*;
import org.altusmetrum.altoslib_11.*;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.*;
import android.os.Bundle;
import android.support.v4.app.Fragment;
//import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.location.Location;
import android.content.*;
class RocketOnline implements Comparable {
Marker marker;
int serial;
long last_packet;
int size;
void set_position(AltosLatLon position, long last_packet) {
marker.setPosition(new LatLng(position.lat, position.lon));
this.last_packet = last_packet;
}
private Bitmap rocket_bitmap(Context context, String text) {
/* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
*/
Bitmap orig_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.rocket);
Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setTextSize(40);
paint.setColor(0xff000000);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int width = bounds.right - bounds.left;
int height = bounds.bottom - bounds.top;
float x = bitmap.getWidth() / 2.0f - width / 2.0f;
float y = bitmap.getHeight() / 2.0f - height / 2.0f;
size = bitmap.getWidth();
canvas.drawText(text, 0, text.length(), x, y, paint);
return bitmap;
}
public void remove() {
marker.remove();
}
public int compareTo(Object o) {
RocketOnline other = (RocketOnline) o;
long diff = last_packet - other.last_packet;
if (diff > 0)
return 1;
if (diff < 0)
return -1;
return 0;
}
RocketOnline(Context context, int serial, GoogleMap map, double lat, double lon, long last_packet) {
this.serial = serial;
String name = String.format("%d", serial);
this.marker = map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(rocket_bitmap(context, name)))
.position(new LatLng(lat, lon))
.visible(true));
this.last_packet = last_packet;
}
}
public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarkerClickListener, GoogleMap.OnMapClickListener, AltosMapTypeListener {
public SupportMapFragment mMapFragment;
private GoogleMap mMap;
private boolean mapLoaded = false;
Context context;
private HashMap<Integer,RocketOnline> rockets = new HashMap<Integer,RocketOnline>();
private Marker mPadMarker;
private boolean pad_set;
private Polyline mPolyline;
private View map_view;
private double mapAccuracy = -1;
private AltosLatLon my_position = null;
private AltosLatLon target_position = null;
private AltosDroid altos_droid;
public void onCreateView(AltosDroid altos_droid) {
this.altos_droid = altos_droid;
final int map_type = AltosPreferences.map_type();
AltosPreferences.register_map_type_listener(this);
mMapFragment = new SupportMapFragment() {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupMap(map_type);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
map_view = super.onCreateView(inflater, container, savedInstanceState);
return map_view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
map_view = null;
}
};
}
public void onDestroyView() {
AltosPreferences.unregister_map_type_listener(this);
}
private double pixel_distance(LatLng a, LatLng b) {
Projection projection = mMap.getProjection();
Point a_pt = projection.toScreenLocation(a);
Point b_pt = projection.toScreenLocation(b);
return Math.hypot((double) (a_pt.x - b_pt.x), (double) (a_pt.y - b_pt.y));
}
private RocketOnline[] sorted_rockets() {
RocketOnline[] rocket_array = rockets.values().toArray(new RocketOnline[0]);
Arrays.sort(rocket_array);
return rocket_array;
}
public void onMapClick(LatLng lat_lng) {
ArrayList<Integer> near = new ArrayList<Integer>();
for (RocketOnline rocket : sorted_rockets()) {
LatLng pos = rocket.marker.getPosition();
if (pos == null)
continue;
double distance = pixel_distance(lat_lng, pos);
if (distance < rocket.size * 2)
near.add(rocket.serial);
}
if (near.size() != 0)
altos_droid.touch_trackers(near.toArray(new Integer[0]));
}
public boolean onMarkerClick(Marker marker) {
onMapClick(marker.getPosition());
return true;
}
public void setupMap(int map_type) {
mMap = mMapFragment.getMap();
if (mMap != null) {
map_type_changed(map_type);
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setTiltGesturesEnabled(false);
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapClickListener(this);
mPadMarker = mMap.addMarker(
new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
.position(new LatLng(0,0))
.visible(false)
);
mPolyline = mMap.addPolyline(
new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
.width(20)
.color(Color.BLUE)
.visible(false)
);
mapLoaded = true;
}
}
public void center(double lat, double lon, double accuracy) {
if (mMap == null)
return;
if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
mapAccuracy = accuracy;
}
}
private void set_rocket(int serial, AltosState state) {
RocketOnline rocket;
if (state.gps == null || state.gps.lat == AltosLib.MISSING)
return;
if (mMap == null)
return;
if (rockets.containsKey(serial)) {
rocket = rockets.get(serial);
rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
} else {
rocket = new RocketOnline(context,
serial,
mMap, state.gps.lat, state.gps.lon,
state.received_time);
rockets.put(serial, rocket);
}
}
private void remove_rocket(int serial) {
RocketOnline rocket = rockets.get(serial);
rocket.remove();
rockets.remove(serial);
}
public void set_visible(boolean visible) {
if (map_view == null)
return;
if (visible)
map_view.setVisibility(View.VISIBLE);
else
map_view.setVisibility(View.GONE);
}
public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
if (telem_state != null) {
for (int serial : rockets.keySet()) {
if (!telem_state.states.containsKey(serial))
remove_rocket(serial);
}
for (int serial : telem_state.states.keySet()) {
set_rocket(serial, telem_state.states.get(serial));
}
}
if (state != null) {
if (mapLoaded) {
if (!pad_set && state.pad_lat != AltosLib.MISSING) {
pad_set = true;
mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
mPadMarker.setVisible(true);
}
}
if (state.gps != null && state.gps.lat != AltosLib.MISSING) {
target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
if (state.gps.locked && state.gps.nsat >= 4)
center (state.gps.lat, state.gps.lon, 10);
}
}
if (receiver != null) {
double accuracy;
if (receiver.hasAccuracy())
accuracy = receiver.getAccuracy();
else
accuracy = 1000;
my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
center (my_position.lat, my_position.lon, accuracy);
}
if (my_position != null && target_position != null && mPolyline != null) {
mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
mPolyline.setVisible(true);
}
}
public void map_type_changed(int map_type) {
if (mMap != null) {
if (map_type == AltosMap.maptype_hybrid)
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
else if (map_type == AltosMap.maptype_satellite)
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
else if (map_type == AltosMap.maptype_terrain)
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
else
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
public AltosMapOnline(Context context) {
this.context = context;
}
}
|