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_14.*;
25 import com.google.android.gms.maps.*;
26 import com.google.android.gms.maps.model.*;
28 import android.graphics.Color;
29 import android.graphics.*;
30 import android.os.Bundle;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.location.Location;
35 import android.content.*;
37 class RocketOnline implements Comparable {
43 void set_position(AltosLatLon position, long last_packet) {
44 marker.setPosition(new LatLng(position.lat, position.lon));
45 this.last_packet = last_packet;
48 private Bitmap rocket_bitmap(Context context, String text) {
50 /* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
52 Bitmap orig_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.rocket);
53 Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true);
55 Canvas canvas = new Canvas(bitmap);
56 Paint paint = new Paint();
57 paint.setTextSize(40);
58 paint.setColor(0xff000000);
60 Rect bounds = new Rect();
61 paint.getTextBounds(text, 0, text.length(), bounds);
63 int width = bounds.right - bounds.left;
64 int height = bounds.bottom - bounds.top;
66 float x = bitmap.getWidth() / 2.0f - width / 2.0f;
67 float y = bitmap.getHeight() / 2.0f - height / 2.0f;
69 size = bitmap.getWidth();
71 canvas.drawText(text, 0, text.length(), x, y, paint);
75 public void remove() {
79 public int compareTo(Object o) {
80 RocketOnline other = (RocketOnline) o;
82 long diff = last_packet - other.last_packet;
91 RocketOnline(Context context, int serial, GoogleMap map, double lat, double lon, long last_packet) {
93 String name = String.format("%d", serial);
94 this.marker = map.addMarker(new MarkerOptions()
95 .icon(BitmapDescriptorFactory.fromBitmap(rocket_bitmap(context, name)))
96 .position(new LatLng(lat, lon))
98 this.last_packet = last_packet;
102 public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarkerClickListener, GoogleMap.OnMapClickListener, OnMapReadyCallback, AltosMapTypeListener {
103 public AltosOnlineMapFragment mMapFragment;
104 private GoogleMap mMap;
105 private boolean mapLoaded = false;
108 private HashMap<Integer,RocketOnline> rockets = new HashMap<Integer,RocketOnline>();
109 private Marker mPadMarker;
110 private boolean pad_set;
111 private Polyline mPolyline;
113 private double mapAccuracy = -1;
115 private AltosLatLon my_position = null;
116 private AltosLatLon target_position = null;
118 private AltosDroid altos_droid;
120 public static class AltosOnlineMapFragment extends SupportMapFragment {
124 public AltosOnlineMapFragment(AltosMapOnline c) {
128 public AltosOnlineMapFragment() {
132 public void onActivityCreated(Bundle savedInstanceState) {
133 super.onActivityCreated(savedInstanceState);
138 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
139 map_view = super.onCreateView(inflater, container, savedInstanceState);
143 public void onDestroyView() {
144 super.onDestroyView();
147 public void set_visible(boolean visible) {
148 if (map_view == null)
151 map_view.setVisibility(View.VISIBLE);
153 map_view.setVisibility(View.GONE);
157 public void onCreateView(AltosDroid altos_droid) {
158 this.altos_droid = altos_droid;
159 AltosPreferences.register_map_type_listener(this);
160 mMapFragment = new AltosOnlineMapFragment(this);
163 public void onDestroyView() {
164 AltosPreferences.unregister_map_type_listener(this);
167 private double pixel_distance(LatLng a, LatLng b) {
168 Projection projection = mMap.getProjection();
170 Point a_pt = projection.toScreenLocation(a);
171 Point b_pt = projection.toScreenLocation(b);
173 return Math.hypot((double) (a_pt.x - b_pt.x), (double) (a_pt.y - b_pt.y));
176 private RocketOnline[] sorted_rockets() {
177 synchronized(rockets) {
178 RocketOnline[] rocket_array = rockets.values().toArray(new RocketOnline[0]);
180 Arrays.sort(rocket_array);
185 public void onMapClick(LatLng lat_lng) {
186 ArrayList<Integer> near = new ArrayList<Integer>();
188 for (RocketOnline rocket : sorted_rockets()) {
189 LatLng pos = rocket.marker.getPosition();
194 double distance = pixel_distance(lat_lng, pos);
195 if (distance < rocket.size * 2)
196 near.add(rocket.serial);
199 if (near.size() != 0)
200 altos_droid.touch_trackers(near.toArray(new Integer[0]));
203 public boolean onMarkerClick(Marker marker) {
204 onMapClick(marker.getPosition());
209 position_permission() {
211 mMap.setMyLocationEnabled(true);
215 public void onMapReady(GoogleMap googleMap) {
216 final int map_type = AltosPreferences.map_type();
219 map_type_changed(map_type);
220 if (altos_droid.have_location_permission)
221 mMap.setMyLocationEnabled(true);
223 altos_droid.tell_map_permission(this);
224 mMap.getUiSettings().setTiltGesturesEnabled(false);
225 mMap.getUiSettings().setZoomControlsEnabled(false);
226 mMap.setOnMarkerClickListener(this);
227 mMap.setOnMapClickListener(this);
229 mPadMarker = mMap.addMarker(
230 new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
231 .position(new LatLng(0,0))
235 mPolyline = mMap.addPolyline(
236 new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
246 public void center(double lat, double lon, double accuracy) {
250 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
251 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
252 mapAccuracy = accuracy;
256 private void set_rocket(int serial, AltosState state) {
259 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
265 synchronized(rockets) {
266 if (rockets.containsKey(serial)) {
267 rocket = rockets.get(serial);
268 rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
270 rocket = new RocketOnline(context,
272 mMap, state.gps.lat, state.gps.lon,
273 state.received_time);
274 rockets.put(serial, rocket);
279 private void remove_rocket(int serial) {
280 synchronized(rockets) {
281 RocketOnline rocket = rockets.get(serial);
283 rockets.remove(serial);
287 public void set_visible(boolean visible) {
288 if (mMapFragment != null)
289 mMapFragment.set_visible(visible);
292 public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
294 if (telem_state != null) {
295 synchronized(rockets) {
296 for (int serial : rockets.keySet()) {
297 if (!telem_state.containsKey(serial))
298 remove_rocket(serial);
301 for (int serial : telem_state.keySet()) {
302 set_rocket(serial, telem_state.get(serial));
309 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
311 mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
312 mPadMarker.setVisible(true);
315 if (state.gps != null && state.gps.lat != AltosLib.MISSING) {
317 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
318 if (state.gps.locked && state.gps.nsat >= 4)
319 center (state.gps.lat, state.gps.lon, 10);
323 if (receiver != null) {
326 if (receiver.hasAccuracy())
327 accuracy = receiver.getAccuracy();
331 my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
332 center (my_position.lat, my_position.lon, accuracy);
335 if (my_position != null && target_position != null && mPolyline != null) {
336 mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
337 mPolyline.setVisible(true);
342 public void map_type_changed(int map_type) {
344 if (map_type == AltosMap.maptype_hybrid)
345 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
346 else if (map_type == AltosMap.maptype_satellite)
347 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
348 else if (map_type == AltosMap.maptype_terrain)
349 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
351 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
355 public AltosMapOnline(Context context) {
356 this.context = context;