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;
20 import java.util.Arrays;
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 Marker mRocketMarker;
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 Bitmap rocket_bitmap(String text) {
67 /* From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
69 Bitmap orig_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rocket);
70 Bitmap bitmap = orig_bitmap.copy(Bitmap.Config.ARGB_8888, true);
72 Canvas canvas = new Canvas(bitmap);
73 Paint paint = new Paint();
74 paint.setTextSize(40);
75 paint.setColor(0xff000000);
77 Rect bounds = new Rect();
78 paint.getTextBounds(text, 0, text.length(), bounds);
80 int width = bounds.right - bounds.left;
81 int height = bounds.bottom - bounds.top;
83 float x = bitmap.getWidth() / 2.0f - width / 2.0f;
84 float y = bitmap.getHeight() / 2.0f - height / 2.0f;
86 AltosDebug.debug("map label x %f y %f\n", x, y);
88 canvas.drawText(text, 0, text.length(), x, y, paint);
92 private Marker rocket_marker(int serial, double lat, double lon) {
93 Bitmap bitmap = rocket_bitmap(String.format("%d", serial));
95 return mMap.addMarker(new MarkerOptions()
96 .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
97 .position(new LatLng(lat, lon))
102 public void onCreate(Bundle savedInstanceState) {
103 super.onCreate(savedInstanceState);
105 mMapFragment = new SupportMapFragment() {
107 public void onActivityCreated(Bundle savedInstanceState) {
108 super.onActivityCreated(savedInstanceState);
116 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
117 View v = inflater.inflate(R.layout.tab_map, container, false);
118 mDistanceView = (TextView)v.findViewById(R.id.distance_value);
119 mBearingView = (TextView)v.findViewById(R.id.bearing_value);
120 mTargetLatitudeView = (TextView)v.findViewById(R.id.target_lat_value);
121 mTargetLongitudeView = (TextView)v.findViewById(R.id.target_lon_value);
122 mReceiverLatitudeView = (TextView)v.findViewById(R.id.receiver_lat_value);
123 mReceiverLongitudeView = (TextView)v.findViewById(R.id.receiver_lon_value);
128 public void onActivityCreated(Bundle savedInstanceState) {
129 super.onActivityCreated(savedInstanceState);
130 getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
133 private void setupMap() {
134 mMap = mMapFragment.getMap();
136 set_map_type(altos_droid.map_type);
137 mMap.setMyLocationEnabled(true);
138 mMap.getUiSettings().setTiltGesturesEnabled(false);
139 mMap.getUiSettings().setZoomControlsEnabled(false);
141 Bitmap label_bitmap = rocket_bitmap("hello");
143 mRocketMarker = rocket_marker(1800,0,0);
145 mPadMarker = mMap.addMarker(
146 new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
147 .position(new LatLng(0,0))
151 mPolyline = mMap.addPolyline(
152 new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
162 private void center(double lat, double lon, double accuracy) {
163 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
164 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
165 mapAccuracy = accuracy;
169 public String tab_name() { return "map"; }
171 public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
172 if (from_receiver != null) {
173 mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
174 set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
179 if (state.gps != null) {
180 mRocketMarker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
181 mRocketMarker.setTitle("hello world");
182 mRocketMarker.setSnippet("hello");
183 mRocketMarker.setVisible(true);
185 mPolyline.setPoints(Arrays.asList(new LatLng(state.pad_lat, state.pad_lon), new LatLng(state.gps.lat, state.gps.lon)));
186 mPolyline.setVisible(true);
189 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
191 mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
192 mPadMarker.setVisible(true);
195 if (state.gps != null) {
196 mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
197 mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "E", "W"));
198 if (state.gps.locked && state.gps.nsat >= 4)
199 center (state.gps.lat, state.gps.lon, 10);
203 if (receiver != null) {
206 if (receiver.hasAccuracy())
207 accuracy = receiver.getAccuracy();
210 mReceiverLatitudeView.setText(AltosDroid.pos(receiver.getLatitude(), "N", "S"));
211 mReceiverLongitudeView.setText(AltosDroid.pos(receiver.getLongitude(), "E", "W"));
212 center (receiver.getLatitude(), receiver.getLongitude(), accuracy);
217 public void set_map_type(int map_type) {
219 if (map_type == AltosMap.maptype_hybrid)
220 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
221 else if (map_type == AltosMap.maptype_satellite)
222 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
223 else if (map_type == AltosMap.maptype_terrain)
224 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
226 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);