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;
46 public class TabMap extends AltosDroidTab {
47 private SupportMapFragment mMapFragment;
48 private GoogleMap mMap;
49 private boolean mapLoaded = false;
51 private HashMap<Integer,Marker> rockets = new HashMap<Integer,Marker>();
52 private Marker mPadMarker;
53 private boolean pad_set;
54 private Polyline mPolyline;
56 private TextView mDistanceView;
57 private TextView mBearingView;
58 private TextView mTargetLatitudeView;
59 private TextView mTargetLongitudeView;
60 private TextView mReceiverLatitudeView;
61 private TextView mReceiverLongitudeView;
63 private double mapAccuracy = -1;
65 private AltosLatLon my_position = null;
66 private AltosLatLon target_position = null;
68 private Bitmap rocket_bitmap(String text) {
70 /* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
72 Bitmap orig_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rocket);
73 Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true);
75 Canvas canvas = new Canvas(bitmap);
76 Paint paint = new Paint();
77 paint.setTextSize(40);
78 paint.setColor(0xff000000);
80 Rect bounds = new Rect();
81 paint.getTextBounds(text, 0, text.length(), bounds);
83 int width = bounds.right - bounds.left;
84 int height = bounds.bottom - bounds.top;
86 float x = bitmap.getWidth() / 2.0f - width / 2.0f;
87 float y = bitmap.getHeight() / 2.0f - height / 2.0f;
89 AltosDebug.debug("map label x %f y %f\n", x, y);
91 canvas.drawText(text, 0, text.length(), x, y, paint);
95 private Marker rocket_marker(int serial, double lat, double lon) {
96 Bitmap bitmap = rocket_bitmap(String.format("%d", serial));
98 return mMap.addMarker(new MarkerOptions()
99 .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
100 .position(new LatLng(lat, lon))
105 public void onCreate(Bundle savedInstanceState) {
106 super.onCreate(savedInstanceState);
108 mMapFragment = new SupportMapFragment() {
110 public void onActivityCreated(Bundle savedInstanceState) {
111 super.onActivityCreated(savedInstanceState);
119 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
120 View v = inflater.inflate(R.layout.tab_map, container, false);
121 mDistanceView = (TextView)v.findViewById(R.id.distance_value);
122 mBearingView = (TextView)v.findViewById(R.id.bearing_value);
123 mTargetLatitudeView = (TextView)v.findViewById(R.id.target_lat_value);
124 mTargetLongitudeView = (TextView)v.findViewById(R.id.target_lon_value);
125 mReceiverLatitudeView = (TextView)v.findViewById(R.id.receiver_lat_value);
126 mReceiverLongitudeView = (TextView)v.findViewById(R.id.receiver_lon_value);
131 public void onActivityCreated(Bundle savedInstanceState) {
132 super.onActivityCreated(savedInstanceState);
133 getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
136 private void setupMap() {
137 mMap = mMapFragment.getMap();
139 set_map_type(altos_droid.map_type);
140 mMap.setMyLocationEnabled(true);
141 mMap.getUiSettings().setTiltGesturesEnabled(false);
142 mMap.getUiSettings().setZoomControlsEnabled(false);
144 Bitmap label_bitmap = rocket_bitmap("hello");
146 mPadMarker = mMap.addMarker(
147 new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
148 .position(new LatLng(0,0))
152 mPolyline = mMap.addPolyline(
153 new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
163 private void center(double lat, double lon, double accuracy) {
164 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
165 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
166 mapAccuracy = accuracy;
170 public String tab_name() { return "map"; }
172 private void set_rocket(int serial, AltosState state) {
175 if (state.gps == null || state.gps.lat == AltosLib.MISSING)
178 if (rockets.containsKey(serial)) {
179 marker = rockets.get(serial);
180 marker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
182 marker = rocket_marker(serial, state.gps.lat, state.gps.lon);
183 rockets.put(serial, marker);
184 marker.setVisible(true);
188 private void remove_rocket(int serial) {
189 Marker marker = rockets.get(serial);
191 rockets.remove(serial);
194 public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
195 if (from_receiver != null) {
196 mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
197 set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
200 if (telem_state != null) {
201 for (int serial : rockets.keySet()) {
202 if (!telem_state.states.containsKey(serial))
203 remove_rocket(serial);
206 for (int serial : telem_state.states.keySet()) {
207 set_rocket(serial, telem_state.states.get(serial));
213 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
215 mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
216 mPadMarker.setVisible(true);
219 if (state.gps != null) {
221 target_position = new AltosLatLon(state.gps.lat, state.gps.lon);
223 mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
224 mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "E", "W"));
225 if (state.gps.locked && state.gps.nsat >= 4)
226 center (state.gps.lat, state.gps.lon, 10);
230 if (receiver != null) {
233 if (receiver.hasAccuracy())
234 accuracy = receiver.getAccuracy();
238 my_position = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
239 mReceiverLatitudeView.setText(AltosDroid.pos(my_position.lat, "N", "S"));
240 mReceiverLongitudeView.setText(AltosDroid.pos(my_position.lon, "E", "W"));
241 center (my_position.lat, my_position.lon, accuracy);
244 if (my_position != null && target_position != null) {
245 mPolyline.setPoints(Arrays.asList(new LatLng(my_position.lat, my_position.lon), new LatLng(target_position.lat, target_position.lon)));
246 mPolyline.setVisible(true);
251 public void set_map_type(int map_type) {
253 if (map_type == AltosMap.maptype_hybrid)
254 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
255 else if (map_type == AltosMap.maptype_satellite)
256 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
257 else if (map_type == AltosMap.maptype_terrain)
258 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
260 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);