2 * Copyright © 2013 Mike Beattie <mike@ethernal.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.AltosDroid;
23 import org.altusmetrum.altoslib_12.*;
25 import com.google.android.gms.maps.*;
26 import com.google.android.gms.maps.model.*;
28 import android.app.Activity;
29 import android.graphics.Color;
30 import android.graphics.*;
31 import android.os.Bundle;
32 import android.support.v4.app.Fragment;
33 //import android.support.v4.app.FragmentTransaction;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.TextView;
38 import android.location.Location;
39 import android.content.*;
41 class RocketOnline implements Comparable {
47 void set_position(AltosLatLon position, long last_packet) {
48 marker.setPosition(new LatLng(position.lat, position.lon));
49 this.last_packet = last_packet;
52 private Bitmap rocket_bitmap(Context context, String text) {
54 /* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
56 Bitmap orig_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.rocket);
57 Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true);
59 Canvas canvas = new Canvas(bitmap);
60 Paint paint = new Paint();
61 paint.setTextSize(40);
62 paint.setColor(0xff000000);
64 Rect bounds = new Rect();
65 paint.getTextBounds(text, 0, text.length(), bounds);
67 int width = bounds.right - bounds.left;
68 int height = bounds.bottom - bounds.top;
70 float x = bitmap.getWidth() / 2.0f - width / 2.0f;
71 float y = bitmap.getHeight() / 2.0f - height / 2.0f;
73 size = bitmap.getWidth();
75 canvas.drawText(text, 0, text.length(), x, y, paint);
79 public void remove() {
83 public int compareTo(Object o) {
84 RocketOnline other = (RocketOnline) o;
86 long diff = last_packet - other.last_packet;
95 RocketOnline(Context context, int serial, GoogleMap map, double lat, double lon, long last_packet) {
97 String name = String.format("%d", serial);
98 this.marker = map.addMarker(new MarkerOptions()
99 .icon(BitmapDescriptorFactory.fromBitmap(rocket_bitmap(context, name)))
100 .position(new LatLng(lat, lon))
102 this.last_packet = last_packet;
106 public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarkerClickListener, GoogleMap.OnMapClickListener, AltosMapTypeListener {
107 public SupportMapFragment mMapFragment;
108 private GoogleMap mMap;
109 private boolean mapLoaded = false;
112 private HashMap<Integer,RocketOnline> rockets = new HashMap<Integer,RocketOnline>();
113 private Marker mPadMarker;
114 private boolean pad_set;
115 private Polyline mPolyline;
117 private View map_view;
119 private double mapAccuracy = -1;
121 private AltosLatLon my_position = null;
122 private AltosLatLon target_position = null;
124 private AltosDroid altos_droid;
126 public void onCreateView(AltosDroid altos_droid) {
127 this.altos_droid = altos_droid;
128 final int map_type = AltosPreferences.map_type();
129 AltosPreferences.register_map_type_listener(this);
130 mMapFragment = new SupportMapFragment() {
132 public void onActivityCreated(Bundle savedInstanceState) {
133 super.onActivityCreated(savedInstanceState);
137 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
138 map_view = super.onCreateView(inflater, container, savedInstanceState);
142 public void onDestroyView() {
143 super.onDestroyView();
149 public void onDestroyView() {
150 AltosPreferences.unregister_map_type_listener(this);
153 private double pixel_distance(LatLng a, LatLng b) {
154 Projection projection = mMap.getProjection();
156 Point a_pt = projection.toScreenLocation(a);
157 Point b_pt = projection.toScreenLocation(b);
159 return Math.hypot((double) (a_pt.x - b_pt.x), (double) (a_pt.y - b_pt.y));
162 private RocketOnline[] sorted_rockets() {
163 RocketOnline[] rocket_array = rockets.values().toArray(new RocketOnline[0]);
165 Arrays.sort(rocket_array);
169 public void onMapClick(LatLng lat_lng) {
170 ArrayList<Integer> near = new ArrayList<Integer>();
172 for (RocketOnline rocket : sorted_rockets()) {
173 LatLng pos = rocket.marker.getPosition();
178 double distance = pixel_distance(lat_lng, pos);
179 if (distance < rocket.size * 2)
180 near.add(rocket.serial);
183 if (near.size() != 0)
184 altos_droid.touch_trackers(near.toArray(new Integer[0]));
187 public boolean onMarkerClick(Marker marker) {
188 onMapClick(marker.getPosition());
192 public void setupMap(int map_type) {
193 mMap = mMapFragment.getMap();
195 map_type_changed(map_type);
196 mMap.setMyLocationEnabled(true);
197 mMap.getUiSettings().setTiltGesturesEnabled(false);
198 mMap.getUiSettings().setZoomControlsEnabled(false);
199 mMap.setOnMarkerClickListener(this);
200 mMap.setOnMapClickListener(this);
202 mPadMarker = mMap.addMarker(
203 new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
204 .position(new LatLng(0,0))
208 mPolyline = mMap.addPolyline(
209 new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
219 public void center(double lat, double lon, double accuracy) {
223 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
224 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
225 mapAccuracy = accuracy;
229 private void set_rocket(int serial, AltosState state) {
232 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
238 if (rockets.containsKey(serial)) {
239 rocket = rockets.get(serial);
240 rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
242 rocket = new RocketOnline(context,
244 mMap, state.gps.lat, state.gps.lon,
245 state.received_time);
246 rockets.put(serial, rocket);
250 private void remove_rocket(int serial) {
251 RocketOnline rocket = rockets.get(serial);
253 rockets.remove(serial);
256 public void set_visible(boolean visible) {
257 if (map_view == null)
260 map_view.setVisibility(View.VISIBLE);
262 map_view.setVisibility(View.GONE);
265 public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
267 if (telem_state != null) {
268 for (int serial : rockets.keySet()) {
269 if (!telem_state.states.containsKey(serial))
270 remove_rocket(serial);
273 for (int serial : telem_state.states.keySet()) {
274 set_rocket(serial, telem_state.states.get(serial));
280 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
282 mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
283 mPadMarker.setVisible(true);
286 if (state.gps != null && state.gps.lat != AltosLib.MISSING) {
288 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
289 if (state.gps.locked && state.gps.nsat >= 4)
290 center (state.gps.lat, state.gps.lon, 10);
294 if (receiver != null) {
297 if (receiver.hasAccuracy())
298 accuracy = receiver.getAccuracy();
302 my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
303 center (my_position.lat, my_position.lon, accuracy);
306 if (my_position != null && target_position != null && mPolyline != null) {
307 mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
308 mPolyline.setVisible(true);
313 public void map_type_changed(int map_type) {
315 if (map_type == AltosMap.maptype_hybrid)
316 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
317 else if (map_type == AltosMap.maptype_satellite)
318 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
319 else if (map_type == AltosMap.maptype_terrain)
320 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
322 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
326 public AltosMapOnline(Context context) {
327 this.context = context;