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.CameraUpdateFactory;
25 import com.google.android.gms.maps.GoogleMap;
26 import com.google.android.gms.maps.SupportMapFragment;
27 import com.google.android.gms.maps.model.BitmapDescriptorFactory;
28 import com.google.android.gms.maps.model.LatLng;
29 import com.google.android.gms.maps.model.Marker;
30 import com.google.android.gms.maps.model.MarkerOptions;
31 import com.google.android.gms.maps.model.Polyline;
32 import com.google.android.gms.maps.model.PolylineOptions;
34 import android.app.Activity;
35 import android.graphics.Color;
36 import android.graphics.*;
37 import android.os.Bundle;
38 import android.support.v4.app.Fragment;
39 //import android.support.v4.app.FragmentTransaction;
40 import android.view.LayoutInflater;
41 import android.view.View;
42 import android.view.ViewGroup;
43 import android.widget.TextView;
44 import android.location.Location;
45 import android.content.*;
47 class RocketOnline implements Comparable {
51 void set_position(AltosLatLon position, long last_packet) {
52 marker.setPosition(new LatLng(position.lat, position.lon));
53 this.last_packet = last_packet;
56 private Bitmap rocket_bitmap(Context context, String text) {
58 /* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
60 Bitmap orig_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.rocket);
61 Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true);
63 Canvas canvas = new Canvas(bitmap);
64 Paint paint = new Paint();
65 paint.setTextSize(40);
66 paint.setColor(0xff000000);
68 Rect bounds = new Rect();
69 paint.getTextBounds(text, 0, text.length(), bounds);
71 int width = bounds.right - bounds.left;
72 int height = bounds.bottom - bounds.top;
74 float x = bitmap.getWidth() / 2.0f - width / 2.0f;
75 float y = bitmap.getHeight() / 2.0f - height / 2.0f;
77 canvas.drawText(text, 0, text.length(), x, y, paint);
81 public void remove() {
85 public int compareTo(Object o) {
86 RocketOnline other = (RocketOnline) o;
88 long diff = last_packet - other.last_packet;
97 RocketOnline(Context context, String name, GoogleMap map, double lat, double lon, long last_packet) {
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 {
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 public void onCreateView(final int map_type) {
125 mMapFragment = new SupportMapFragment() {
127 public void onActivityCreated(Bundle savedInstanceState) {
128 super.onActivityCreated(savedInstanceState);
132 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
133 map_view = super.onCreateView(inflater, container, savedInstanceState);
137 public void onDestroyView() {
138 super.onDestroyView();
144 // public void onActivityCreated() {
145 // getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
148 public void setupMap(int map_type) {
149 mMap = mMapFragment.getMap();
151 set_map_type(map_type);
152 mMap.setMyLocationEnabled(true);
153 mMap.getUiSettings().setTiltGesturesEnabled(false);
154 mMap.getUiSettings().setZoomControlsEnabled(false);
156 mPadMarker = mMap.addMarker(
157 new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
158 .position(new LatLng(0,0))
162 mPolyline = mMap.addPolyline(
163 new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
173 public void center(double lat, double lon, double accuracy) {
177 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
178 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
179 mapAccuracy = accuracy;
183 private void set_rocket(int serial, AltosState state) {
186 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
192 if (rockets.containsKey(serial)) {
193 rocket = rockets.get(serial);
194 rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
196 rocket = new RocketOnline(context,
197 String.format("%d", serial),
198 mMap, state.gps.lat, state.gps.lon,
199 state.received_time);
200 rockets.put(serial, rocket);
204 private void remove_rocket(int serial) {
205 RocketOnline rocket = rockets.get(serial);
207 rockets.remove(serial);
210 public void set_visible(boolean visible) {
211 if (map_view == null)
214 map_view.setVisibility(View.VISIBLE);
216 map_view.setVisibility(View.GONE);
219 public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
221 if (telem_state != null) {
222 for (int serial : rockets.keySet()) {
223 if (!telem_state.states.containsKey(serial))
224 remove_rocket(serial);
227 for (int serial : telem_state.states.keySet()) {
228 set_rocket(serial, telem_state.states.get(serial));
234 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
236 mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
237 mPadMarker.setVisible(true);
240 if (state.gps != null) {
242 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
243 if (state.gps.locked && state.gps.nsat >= 4)
244 center (state.gps.lat, state.gps.lon, 10);
248 if (receiver != null) {
251 if (receiver.hasAccuracy())
252 accuracy = receiver.getAccuracy();
256 my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
257 center (my_position.lat, my_position.lon, accuracy);
260 if (my_position != null && target_position != null) {
261 mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
262 mPolyline.setVisible(true);
267 public void set_map_type(int map_type) {
269 if (map_type == AltosMap.maptype_hybrid)
270 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
271 else if (map_type == AltosMap.maptype_satellite)
272 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
273 else if (map_type == AltosMap.maptype_terrain)
274 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
276 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
280 public AltosMapOnline(Context context) {
281 this.context = context;