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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.AltosDroid;
22 import org.altusmetrum.altoslib_11.*;
24 import com.google.android.gms.maps.*;
25 import com.google.android.gms.maps.model.*;
27 import android.app.Activity;
28 import android.graphics.Color;
29 import android.graphics.*;
30 import android.os.Bundle;
31 import android.support.v4.app.Fragment;
32 //import android.support.v4.app.FragmentTransaction;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.TextView;
37 import android.location.Location;
38 import android.content.*;
40 class RocketOnline implements Comparable {
46 void set_position(AltosLatLon position, long last_packet) {
47 marker.setPosition(new LatLng(position.lat, position.lon));
48 this.last_packet = last_packet;
51 private Bitmap rocket_bitmap(Context context, String text) {
53 /* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
55 Bitmap orig_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.rocket);
56 Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true);
58 Canvas canvas = new Canvas(bitmap);
59 Paint paint = new Paint();
60 paint.setTextSize(40);
61 paint.setColor(0xff000000);
63 Rect bounds = new Rect();
64 paint.getTextBounds(text, 0, text.length(), bounds);
66 int width = bounds.right - bounds.left;
67 int height = bounds.bottom - bounds.top;
69 float x = bitmap.getWidth() / 2.0f - width / 2.0f;
70 float y = bitmap.getHeight() / 2.0f - height / 2.0f;
72 size = bitmap.getWidth();
74 canvas.drawText(text, 0, text.length(), x, y, paint);
78 public void remove() {
82 public int compareTo(Object o) {
83 RocketOnline other = (RocketOnline) o;
85 long diff = last_packet - other.last_packet;
94 RocketOnline(Context context, int serial, GoogleMap map, double lat, double lon, long last_packet) {
96 String name = String.format("%d", serial);
97 this.marker = map.addMarker(new MarkerOptions()
98 .icon(BitmapDescriptorFactory.fromBitmap(rocket_bitmap(context, name)))
99 .position(new LatLng(lat, lon))
101 this.last_packet = last_packet;
105 public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarkerClickListener, GoogleMap.OnMapClickListener, AltosMapTypeListener {
106 public SupportMapFragment mMapFragment;
107 private GoogleMap mMap;
108 private boolean mapLoaded = false;
111 private HashMap<Integer,RocketOnline> rockets = new HashMap<Integer,RocketOnline>();
112 private Marker mPadMarker;
113 private boolean pad_set;
114 private Polyline mPolyline;
116 private View map_view;
118 private double mapAccuracy = -1;
120 private AltosLatLon my_position = null;
121 private AltosLatLon target_position = null;
123 private AltosDroid altos_droid;
125 public void onCreateView(AltosDroid altos_droid) {
126 this.altos_droid = altos_droid;
127 final int map_type = AltosPreferences.map_type();
128 AltosPreferences.register_map_type_listener(this);
129 mMapFragment = new SupportMapFragment() {
131 public void onActivityCreated(Bundle savedInstanceState) {
132 super.onActivityCreated(savedInstanceState);
136 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
137 map_view = super.onCreateView(inflater, container, savedInstanceState);
141 public void onDestroyView() {
142 super.onDestroyView();
148 public void onDestroyView() {
149 AltosPreferences.unregister_map_type_listener(this);
152 private double pixel_distance(LatLng a, LatLng b) {
153 Projection projection = mMap.getProjection();
155 Point a_pt = projection.toScreenLocation(a);
156 Point b_pt = projection.toScreenLocation(b);
158 return Math.hypot((double) (a_pt.x - b_pt.x), (double) (a_pt.y - b_pt.y));
161 private RocketOnline[] sorted_rockets() {
162 RocketOnline[] rocket_array = rockets.values().toArray(new RocketOnline[0]);
164 Arrays.sort(rocket_array);
168 public void onMapClick(LatLng lat_lng) {
169 ArrayList<Integer> near = new ArrayList<Integer>();
171 for (RocketOnline rocket : sorted_rockets()) {
172 LatLng pos = rocket.marker.getPosition();
177 double distance = pixel_distance(lat_lng, pos);
178 if (distance < rocket.size * 2)
179 near.add(rocket.serial);
182 if (near.size() != 0)
183 altos_droid.touch_trackers(near.toArray(new Integer[0]));
186 public boolean onMarkerClick(Marker marker) {
187 onMapClick(marker.getPosition());
191 public void setupMap(int map_type) {
192 mMap = mMapFragment.getMap();
194 map_type_changed(map_type);
195 mMap.setMyLocationEnabled(true);
196 mMap.getUiSettings().setTiltGesturesEnabled(false);
197 mMap.getUiSettings().setZoomControlsEnabled(false);
198 mMap.setOnMarkerClickListener(this);
199 mMap.setOnMapClickListener(this);
201 mPadMarker = mMap.addMarker(
202 new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
203 .position(new LatLng(0,0))
207 mPolyline = mMap.addPolyline(
208 new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
218 public void center(double lat, double lon, double accuracy) {
222 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
223 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
224 mapAccuracy = accuracy;
228 private void set_rocket(int serial, AltosState state) {
231 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
237 if (rockets.containsKey(serial)) {
238 rocket = rockets.get(serial);
239 rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
241 rocket = new RocketOnline(context,
243 mMap, state.gps.lat, state.gps.lon,
244 state.received_time);
245 rockets.put(serial, rocket);
249 private void remove_rocket(int serial) {
250 RocketOnline rocket = rockets.get(serial);
252 rockets.remove(serial);
255 public void set_visible(boolean visible) {
256 if (map_view == null)
259 map_view.setVisibility(View.VISIBLE);
261 map_view.setVisibility(View.GONE);
264 public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
266 if (telem_state != null) {
267 for (int serial : rockets.keySet()) {
268 if (!telem_state.states.containsKey(serial))
269 remove_rocket(serial);
272 for (int serial : telem_state.states.keySet()) {
273 set_rocket(serial, telem_state.states.get(serial));
279 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
281 mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
282 mPadMarker.setVisible(true);
285 if (state.gps != null && state.gps.lat != AltosLib.MISSING) {
287 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
288 if (state.gps.locked && state.gps.nsat >= 4)
289 center (state.gps.lat, state.gps.lon, 10);
293 if (receiver != null) {
296 if (receiver.hasAccuracy())
297 accuracy = receiver.getAccuracy();
301 my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
302 center (my_position.lat, my_position.lon, accuracy);
305 if (my_position != null && target_position != null && mPolyline != null) {
306 mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
307 mPolyline.setVisible(true);
312 public void map_type_changed(int map_type) {
314 if (map_type == AltosMap.maptype_hybrid)
315 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
316 else if (map_type == AltosMap.maptype_satellite)
317 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
318 else if (map_type == AltosMap.maptype_terrain)
319 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
321 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
325 public AltosMapOnline(Context context) {
326 this.context = context;