altosdroid: Add map polyline between pad and rocket
[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_1.*;
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
44 public class TabMap extends Fragment implements AltosDroidTab {
45         AltosDroid mAltosDroid;
46
47         private SupportMapFragment mMapFragment;
48         private GoogleMap mMap;
49         private boolean mapLoaded = false;
50
51         private Marker mRocketMarker;
52         private Marker mPadMarker;
53         private Polyline mPolyline;
54
55         private TextView mDistanceView;
56         private TextView mBearingView;
57         private TextView mLatitudeView;
58         private TextView mLongitudeView;
59
60         @Override
61         public void onAttach(Activity activity) {
62                 super.onAttach(activity);
63                 mAltosDroid = (AltosDroid) activity;
64                 mAltosDroid.registerTab(this);
65         }
66
67         @Override
68         public void onCreate(Bundle savedInstanceState) {
69                 super.onCreate(savedInstanceState);
70
71                 mMapFragment = new SupportMapFragment() {
72                         @Override
73                         public void onActivityCreated(Bundle savedInstanceState) {
74                                 super.onActivityCreated(savedInstanceState);
75                                 setupMap();
76                         }
77                 };
78
79         }
80
81         @Override
82         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
83                 View v = inflater.inflate(R.layout.tab_map, container, false);
84                 mDistanceView  = (TextView)v.findViewById(R.id.distance_value);
85                 mBearingView   = (TextView)v.findViewById(R.id.bearing_value);
86                 mLatitudeView  = (TextView)v.findViewById(R.id.lat_value);
87                 mLongitudeView = (TextView)v.findViewById(R.id.lon_value);
88                 return v;
89         }
90
91         @Override
92         public void onActivityCreated(Bundle savedInstanceState) {
93                 super.onActivityCreated(savedInstanceState);
94                 getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
95         }
96
97         @Override
98         public void onDestroyView() {
99                 super.onDestroyView();
100
101                 mAltosDroid.unregisterTab(this);
102                 mAltosDroid = null;
103
104                 //Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
105                 //FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
106                 //ft.remove(fragment);
107                 //ft.commit();
108         }
109
110         private void setupMap() {
111                 mMap = mMapFragment.getMap();
112                 if (mMap != null) {
113                         mMap.setMyLocationEnabled(true);
114                         mMap.getUiSettings().setTiltGesturesEnabled(false);
115                         mMap.getUiSettings().setZoomControlsEnabled(false);
116                         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(40.8,-104.7),8));
117
118                         mRocketMarker = mMap.addMarker(
119                                         // From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
120                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.rocket))
121                                                            .position(new LatLng(0,0))
122                                                            .visible(false)
123                                         );
124
125                         mPadMarker = mMap.addMarker(
126                                         new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
127                                                            .position(new LatLng(0,0))
128                                                            .visible(false)
129                                         );
130
131                         mPolyline = mMap.addPolyline(
132                                         new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
133                                                              .width(3)
134                                                              .color(Color.BLUE)
135                                                              .visible(false)
136                                         );
137
138                         mapLoaded = true;
139                 }
140         }
141
142         public void update_ui(AltosState state) {
143                 if (state.from_pad != null) {
144                         mDistanceView.setText(String.format("%6.0f m", state.from_pad.distance));
145                         mBearingView.setText(String.format("%3.0f°", state.from_pad.bearing));
146                 }
147                 mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
148                 mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
149
150                 if (mapLoaded) {
151                         mRocketMarker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
152                         mRocketMarker.setVisible(true);
153
154                         mPolyline.setPoints(Arrays.asList(new LatLng(state.pad_lat, state.pad_lon), new LatLng(state.gps.lat, state.gps.lon)));
155                         mPolyline.setVisible(true);
156
157                         if (state.state == AltosLib.ao_flight_pad) {
158                                 mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
159                                 mPadMarker.setVisible(true);
160                         }
161                 }
162         }
163
164 }