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 {
52 void set_position(AltosLatLon position, long last_packet) {
53 marker.setPosition(new LatLng(position.lat, position.lon));
54 this.last_packet = last_packet;
57 private Bitmap rocket_bitmap(Context context, String text) {
59 /* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
61 Bitmap orig_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.rocket);
62 Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true);
64 Canvas canvas = new Canvas(bitmap);
65 Paint paint = new Paint();
66 paint.setTextSize(40);
67 paint.setColor(0xff000000);
69 Rect bounds = new Rect();
70 paint.getTextBounds(text, 0, text.length(), bounds);
72 int width = bounds.right - bounds.left;
73 int height = bounds.bottom - bounds.top;
75 float x = bitmap.getWidth() / 2.0f - width / 2.0f;
76 float y = bitmap.getHeight() / 2.0f - height / 2.0f;
78 canvas.drawText(text, 0, text.length(), x, y, paint);
82 public void remove() {
86 public int compareTo(Object o) {
87 RocketOnline other = (RocketOnline) o;
89 long diff = last_packet - other.last_packet;
98 RocketOnline(Context context, int serial, GoogleMap map, double lat, double lon, long last_packet) {
100 String name = String.format("%d", serial);
101 this.marker = map.addMarker(new MarkerOptions()
102 .icon(BitmapDescriptorFactory.fromBitmap(rocket_bitmap(context, name)))
103 .position(new LatLng(lat, lon))
105 this.last_packet = last_packet;
109 public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarkerClickListener {
110 public SupportMapFragment mMapFragment;
111 private GoogleMap mMap;
112 private boolean mapLoaded = false;
115 private HashMap<Integer,RocketOnline> rockets = new HashMap<Integer,RocketOnline>();
116 private Marker mPadMarker;
117 private boolean pad_set;
118 private Polyline mPolyline;
120 private View map_view;
122 private double mapAccuracy = -1;
124 private AltosLatLon my_position = null;
125 private AltosLatLon target_position = null;
127 private AltosDroid altos_droid;
129 public void onCreateView(AltosDroid altos_droid) {
130 this.altos_droid = altos_droid;
131 final int map_type = altos_droid.map_type;
132 mMapFragment = new SupportMapFragment() {
134 public void onActivityCreated(Bundle savedInstanceState) {
135 super.onActivityCreated(savedInstanceState);
139 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
140 map_view = super.onCreateView(inflater, container, savedInstanceState);
144 public void onDestroyView() {
145 super.onDestroyView();
151 // public void onActivityCreated() {
152 // getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
155 public boolean onMarkerClick(Marker marker) {
156 for (RocketOnline rocket : rockets.values()) {
157 if (rocket.marker.equals(marker)) {
158 altos_droid.select_tracker(rocket.serial);
165 public void setupMap(int map_type) {
166 mMap = mMapFragment.getMap();
168 set_map_type(map_type);
169 mMap.setMyLocationEnabled(true);
170 mMap.getUiSettings().setTiltGesturesEnabled(false);
171 mMap.getUiSettings().setZoomControlsEnabled(false);
172 mMap.setOnMarkerClickListener(this);
174 mPadMarker = mMap.addMarker(
175 new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
176 .position(new LatLng(0,0))
180 mPolyline = mMap.addPolyline(
181 new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
191 public void center(double lat, double lon, double accuracy) {
195 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
196 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
197 mapAccuracy = accuracy;
201 private void set_rocket(int serial, AltosState state) {
204 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
210 if (rockets.containsKey(serial)) {
211 rocket = rockets.get(serial);
212 rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
214 rocket = new RocketOnline(context,
216 mMap, state.gps.lat, state.gps.lon,
217 state.received_time);
218 rockets.put(serial, rocket);
222 private void remove_rocket(int serial) {
223 RocketOnline rocket = rockets.get(serial);
225 rockets.remove(serial);
228 public void set_visible(boolean visible) {
229 if (map_view == null)
232 map_view.setVisibility(View.VISIBLE);
234 map_view.setVisibility(View.GONE);
237 public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
239 if (telem_state != null) {
240 for (int serial : rockets.keySet()) {
241 if (!telem_state.states.containsKey(serial))
242 remove_rocket(serial);
245 for (int serial : telem_state.states.keySet()) {
246 set_rocket(serial, telem_state.states.get(serial));
252 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
254 mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
255 mPadMarker.setVisible(true);
258 if (state.gps != null) {
260 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
261 if (state.gps.locked && state.gps.nsat >= 4)
262 center (state.gps.lat, state.gps.lon, 10);
266 if (receiver != null) {
269 if (receiver.hasAccuracy())
270 accuracy = receiver.getAccuracy();
274 my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
275 center (my_position.lat, my_position.lon, accuracy);
278 if (my_position != null && target_position != null && mPolyline != null) {
279 mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
280 mPolyline.setVisible(true);
285 public void set_map_type(int map_type) {
287 if (map_type == AltosMap.maptype_hybrid)
288 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
289 else if (map_type == AltosMap.maptype_satellite)
290 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
291 else if (map_type == AltosMap.maptype_terrain)
292 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
294 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
298 public AltosMapOnline(Context context) {
299 this.context = context;