altosdroid: Add rocket and pad map markers
[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
21 import org.altusmetrum.altoslib_1.*;
22
23 import com.google.android.gms.maps.CameraUpdateFactory;
24 import com.google.android.gms.maps.GoogleMap;
25 import com.google.android.gms.maps.SupportMapFragment;
26 import com.google.android.gms.maps.model.BitmapDescriptorFactory;
27 import com.google.android.gms.maps.model.LatLng;
28 import com.google.android.gms.maps.model.Marker;
29 import com.google.android.gms.maps.model.MarkerOptions;
30
31 import android.app.Activity;
32 import android.os.Bundle;
33 import android.support.v4.app.Fragment;
34 import android.support.v4.app.FragmentTransaction;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.TextView;
39
40 public class TabMap extends Fragment implements AltosDroidTab {
41         AltosDroid mAltosDroid;
42
43         private SupportMapFragment mMapFragment;
44         private GoogleMap mMap;
45         private boolean mapLoaded = false;
46
47         private Marker mRocketMarker;
48         private Marker mPadMarker;
49         private TextView mDistanceView;
50         private TextView mBearingView;
51         private TextView mLatitudeView;
52         private TextView mLongitudeView;
53
54         @Override
55         public void onAttach(Activity activity) {
56                 super.onAttach(activity);
57                 mAltosDroid = (AltosDroid) activity;
58                 mAltosDroid.registerTab(this);
59         }
60
61         @Override
62         public void onCreate(Bundle savedInstanceState) {
63                 super.onCreate(savedInstanceState);
64
65                 mMapFragment = new SupportMapFragment() {
66                         @Override
67                         public void onActivityCreated(Bundle savedInstanceState) {
68                                 super.onActivityCreated(savedInstanceState);
69                                 setupMap();
70                         }
71                 };
72
73         }
74
75         @Override
76         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
77                 View v = inflater.inflate(R.layout.tab_map, container, false);
78                 mDistanceView  = (TextView)v.findViewById(R.id.distance_value);
79                 mBearingView   = (TextView)v.findViewById(R.id.bearing_value);
80                 mLatitudeView  = (TextView)v.findViewById(R.id.lat_value);
81                 mLongitudeView = (TextView)v.findViewById(R.id.lon_value);
82                 return v;
83         }
84
85         @Override
86         public void onActivityCreated(Bundle savedInstanceState) {
87                 super.onActivityCreated(savedInstanceState);
88                 getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
89         }
90
91         @Override
92         public void onDestroyView() {
93                 super.onDestroyView();
94
95                 mAltosDroid.unregisterTab(this);
96                 mAltosDroid = null;
97
98                 //Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
99                 //FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
100                 //ft.remove(fragment);
101                 //ft.commit();
102         }
103
104         private void setupMap() {
105                 mMap = mMapFragment.getMap();
106                 if (mMap != null) {
107                         mMap.setMyLocationEnabled(true);
108                         mMap.getUiSettings().setTiltGesturesEnabled(false);
109                         mMap.getUiSettings().setZoomControlsEnabled(false);
110                         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(40.8,-104.7),8));
111
112                         mRocketMarker = mMap.addMarker(
113                                         // From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
114                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.rocket))
115                                                            .position(new LatLng(0,0))
116                                                            .visible(false)
117                                         );
118
119                         mPadMarker = mMap.addMarker(
120                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
121                                                            .position(new LatLng(0,0))
122                                                            .visible(false)
123                                         );
124
125                         mapLoaded = true;
126                 }
127         }
128
129         public void update_ui(AltosState state) {
130                 if (state.from_pad != null) {
131                         mDistanceView.setText(String.format("%6.0f m", state.from_pad.distance));
132                         mBearingView.setText(String.format("%3.0f°", state.from_pad.bearing));
133                 }
134                 mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
135                 mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
136
137                 if (mapLoaded) {
138                         mRocketMarker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
139                         mRocketMarker.setVisible(true);
140
141                         if (state.state == AltosLib.ao_flight_pad) {
142                                 mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
143                                 mPadMarker.setVisible(true);
144                         }
145                 }
146         }
147
148 }