altosdroid: Save selected map type in AltosDroid object
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TabMap.java
1 /*
2  * Copyright © 2013 Mike Beattie <mike@ethernal.org>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.AltosDroid;
19
20 import java.util.Arrays;
21
22 import org.altusmetrum.altoslib_7.*;
23
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;
33
34 import android.app.Activity;
35 import android.graphics.Color;
36 import android.os.Bundle;
37 import android.support.v4.app.Fragment;
38 //import android.support.v4.app.FragmentTransaction;
39 import android.view.LayoutInflater;
40 import android.view.View;
41 import android.view.ViewGroup;
42 import android.widget.TextView;
43 import android.location.Location;
44
45 public class TabMap extends AltosDroidTab {
46         AltosDroid mAltosDroid;
47
48         private SupportMapFragment mMapFragment;
49         private GoogleMap mMap;
50         private boolean mapLoaded = false;
51
52         private Marker mRocketMarker;
53         private Marker mPadMarker;
54         private boolean pad_set;
55         private Polyline mPolyline;
56
57         private TextView mDistanceView;
58         private TextView mBearingView;
59         private TextView mTargetLatitudeView;
60         private TextView mTargetLongitudeView;
61         private TextView mReceiverLatitudeView;
62         private TextView mReceiverLongitudeView;
63
64         private double mapAccuracy = -1;
65
66         @Override
67         public void onAttach(Activity activity) {
68                 super.onAttach(activity);
69                 mAltosDroid = (AltosDroid) activity;
70                 mAltosDroid.registerTab(this);
71         }
72
73         @Override
74         public void onCreate(Bundle savedInstanceState) {
75                 super.onCreate(savedInstanceState);
76
77                 mMapFragment = new SupportMapFragment() {
78                         @Override
79                         public void onActivityCreated(Bundle savedInstanceState) {
80                                 super.onActivityCreated(savedInstanceState);
81                                 setupMap();
82                         }
83                 };
84
85         }
86
87         @Override
88         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
89                 View v = inflater.inflate(R.layout.tab_map, container, false);
90                 mDistanceView  = (TextView)v.findViewById(R.id.distance_value);
91                 mBearingView   = (TextView)v.findViewById(R.id.bearing_value);
92                 mTargetLatitudeView  = (TextView)v.findViewById(R.id.target_lat_value);
93                 mTargetLongitudeView = (TextView)v.findViewById(R.id.target_lon_value);
94                 mReceiverLatitudeView  = (TextView)v.findViewById(R.id.receiver_lat_value);
95                 mReceiverLongitudeView = (TextView)v.findViewById(R.id.receiver_lon_value);
96                 return v;
97         }
98
99         @Override
100         public void onActivityCreated(Bundle savedInstanceState) {
101                 super.onActivityCreated(savedInstanceState);
102                 getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
103         }
104
105         @Override
106         public void onDestroyView() {
107                 super.onDestroyView();
108
109                 mAltosDroid.unregisterTab(this);
110                 mAltosDroid = null;
111
112                 //Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
113                 //FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
114                 //ft.remove(fragment);
115                 //ft.commit();
116         }
117
118         private void setupMap() {
119                 mMap = mMapFragment.getMap();
120                 if (mMap != null) {
121                         set_map_type(mAltosDroid.map_type);
122                         mMap.setMyLocationEnabled(true);
123                         mMap.getUiSettings().setTiltGesturesEnabled(false);
124                         mMap.getUiSettings().setZoomControlsEnabled(false);
125
126                         mRocketMarker = mMap.addMarker(
127                                         // From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
128                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.rocket))
129                                                            .position(new LatLng(0,0))
130                                                            .visible(false)
131                                         );
132
133                         mPadMarker = mMap.addMarker(
134                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
135                                                            .position(new LatLng(0,0))
136                                                            .visible(false)
137                                         );
138
139                         mPolyline = mMap.addPolyline(
140                                         new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
141                                                              .width(3)
142                                                              .color(Color.BLUE)
143                                                              .visible(false)
144                                         );
145
146                         mapLoaded = true;
147                 }
148         }
149
150         private void center(double lat, double lon, double accuracy) {
151                 if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
152                         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon),14));
153                         mapAccuracy = accuracy;
154                 }
155         }
156
157         public String tab_name() { return "map"; }
158
159         public void show(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
160                 if (from_receiver != null) {
161                         mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
162                         set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
163                 }
164
165                 if (state != null) {
166                         if (mapLoaded) {
167                                 if (state.gps != null) {
168                                         mRocketMarker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
169                                         mRocketMarker.setVisible(true);
170
171                                         mPolyline.setPoints(Arrays.asList(new LatLng(state.pad_lat, state.pad_lon), new LatLng(state.gps.lat, state.gps.lon)));
172                                         mPolyline.setVisible(true);
173                                 }
174
175                                 if (!pad_set && state.pad_lat != AltosLib.MISSING) {
176                                         pad_set = true;
177                                         mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
178                                         mPadMarker.setVisible(true);
179                                 }
180                         }
181                         if (state.gps != null) {
182                                 mTargetLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
183                                 mTargetLongitudeView.setText(AltosDroid.pos(state.gps.lon, "E", "W"));
184                                 if (state.gps.locked && state.gps.nsat >= 4)
185                                         center (state.gps.lat, state.gps.lon, 10);
186                         }
187                 }
188
189                 if (receiver != null) {
190                         double accuracy;
191
192                         if (receiver.hasAccuracy())
193                                 accuracy = receiver.getAccuracy();
194                         else
195                                 accuracy = 1000;
196                         mReceiverLatitudeView.setText(AltosDroid.pos(receiver.getLatitude(), "N", "S"));
197                         mReceiverLongitudeView.setText(AltosDroid.pos(receiver.getLongitude(), "E", "W"));
198                         center (receiver.getLatitude(), receiver.getLongitude(), accuracy);
199                 }
200
201         }
202
203         public void set_map_type(int map_type) {
204                 if (mMap != null) {
205                         if (map_type == AltosMap.maptype_hybrid)
206                                 mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
207                         else if (map_type == AltosMap.maptype_satellite)
208                                 mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
209                         else if (map_type == AltosMap.maptype_terrain)
210                                 mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
211                         else
212                                 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
213                 }
214         }
215
216         public TabMap() {
217         }
218 }