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_7.*;
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 {
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 = altos_droid.map_type;
128 mMapFragment = new SupportMapFragment() {
130 public void onActivityCreated(Bundle savedInstanceState) {
131 super.onActivityCreated(savedInstanceState);
135 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
136 map_view = super.onCreateView(inflater, container, savedInstanceState);
140 public void onDestroyView() {
141 super.onDestroyView();
147 // public void onActivityCreated() {
148 // getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
151 private double pixel_distance(LatLng a, LatLng b) {
152 Projection projection = mMap.getProjection();
154 Point a_pt = projection.toScreenLocation(a);
155 Point b_pt = projection.toScreenLocation(b);
157 return Math.hypot((double) (a_pt.x - b_pt.x), (double) (a_pt.y - b_pt.y));
160 private RocketOnline[] sorted_rockets() {
161 RocketOnline[] rocket_array = rockets.values().toArray(new RocketOnline[0]);
163 Arrays.sort(rocket_array);
167 public void onMapClick(LatLng lat_lng) {
168 ArrayList<Integer> near = new ArrayList<Integer>();
170 for (RocketOnline rocket : sorted_rockets()) {
171 LatLng pos = rocket.marker.getPosition();
176 double distance = pixel_distance(lat_lng, pos);
177 if (distance < rocket.size * 2)
178 near.add(rocket.serial);
181 if (near.size() != 0)
182 altos_droid.touch_trackers(near.toArray(new Integer[0]));
185 public boolean onMarkerClick(Marker marker) {
186 onMapClick(marker.getPosition());
190 public void setupMap(int map_type) {
191 mMap = mMapFragment.getMap();
193 set_map_type(map_type);
194 mMap.setMyLocationEnabled(true);
195 mMap.getUiSettings().setTiltGesturesEnabled(false);
196 mMap.getUiSettings().setZoomControlsEnabled(false);
197 mMap.setOnMarkerClickListener(this);
198 mMap.setOnMapClickListener(this);
200 mPadMarker = mMap.addMarker(
201 new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
202 .position(new LatLng(0,0))
206 mPolyline = mMap.addPolyline(
207 new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
217 public void center(double lat, double lon, double accuracy) {
221 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
222 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
223 mapAccuracy = accuracy;
227 private void set_rocket(int serial, AltosState state) {
230 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
236 if (rockets.containsKey(serial)) {
237 rocket = rockets.get(serial);
238 rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
240 rocket = new RocketOnline(context,
242 mMap, state.gps.lat, state.gps.lon,
243 state.received_time);
244 rockets.put(serial, rocket);
248 private void remove_rocket(int serial) {
249 RocketOnline rocket = rockets.get(serial);
251 rockets.remove(serial);
254 public void set_visible(boolean visible) {
255 if (map_view == null)
258 map_view.setVisibility(View.VISIBLE);
260 map_view.setVisibility(View.GONE);
263 public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
265 if (telem_state != null) {
266 for (int serial : rockets.keySet()) {
267 if (!telem_state.states.containsKey(serial))
268 remove_rocket(serial);
271 for (int serial : telem_state.states.keySet()) {
272 set_rocket(serial, telem_state.states.get(serial));
278 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
280 mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
281 mPadMarker.setVisible(true);
284 if (state.gps != null) {
286 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
287 if (state.gps.locked && state.gps.nsat >= 4)
288 center (state.gps.lat, state.gps.lon, 10);
292 if (receiver != null) {
295 if (receiver.hasAccuracy())
296 accuracy = receiver.getAccuracy();
300 my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
301 center (my_position.lat, my_position.lon, accuracy);
304 if (my_position != null && target_position != null && mPolyline != null) {
305 mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
306 mPolyline.setVisible(true);
311 public void set_map_type(int map_type) {
313 if (map_type == AltosMap.maptype_hybrid)
314 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
315 else if (map_type == AltosMap.maptype_satellite)
316 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
317 else if (map_type == AltosMap.maptype_terrain)
318 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
320 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
324 public AltosMapOnline(Context context) {
325 this.context = context;