altosdroid: programmatically create map fragment
[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.LatLng;
27
28 import android.app.Activity;
29 import android.os.Bundle;
30 import android.support.v4.app.Fragment;
31 import android.support.v4.app.FragmentTransaction;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.widget.TextView;
36
37 public class TabMap extends Fragment implements AltosDroidTab {
38         AltosDroid mAltosDroid;
39
40         private SupportMapFragment mMapFragment;
41         private GoogleMap mMap;
42         private boolean mapLoaded = false;
43
44         private TextView mDistanceView;
45         private TextView mBearingView;
46         private TextView mLatitudeView;
47         private TextView mLongitudeView;
48
49         @Override
50         public void onAttach(Activity activity) {
51                 super.onAttach(activity);
52                 mAltosDroid = (AltosDroid) activity;
53                 mAltosDroid.registerTab(this);
54         }
55
56         @Override
57         public void onCreate(Bundle savedInstanceState) {
58                 super.onCreate(savedInstanceState);
59
60                 mMapFragment = new SupportMapFragment() {
61                         @Override
62                         public void onActivityCreated(Bundle savedInstanceState) {
63                                 super.onActivityCreated(savedInstanceState);
64                                 setupMap();
65                         }
66                 };
67
68         }
69
70         @Override
71         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
72                 View v = inflater.inflate(R.layout.tab_map, container, false);
73                 mDistanceView  = (TextView)v.findViewById(R.id.distance_value);
74                 mBearingView   = (TextView)v.findViewById(R.id.bearing_value);
75                 mLatitudeView  = (TextView)v.findViewById(R.id.lat_value);
76                 mLongitudeView = (TextView)v.findViewById(R.id.lon_value);
77                 return v;
78         }
79
80         @Override
81         public void onActivityCreated(Bundle savedInstanceState) {
82                 super.onActivityCreated(savedInstanceState);
83                 getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
84         }
85
86         @Override
87         public void onDestroyView() {
88                 super.onDestroyView();
89
90                 mAltosDroid.unregisterTab(this);
91                 mAltosDroid = null;
92
93                 //Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
94                 //FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
95                 //ft.remove(fragment);
96                 //ft.commit();
97         }
98
99         private void setupMap() {
100                 mMap = mMapFragment.getMap();
101                 if (mMap != null) {
102                         mMap.setMyLocationEnabled(true);
103                         mMap.getUiSettings().setTiltGesturesEnabled(false);
104                         mMap.getUiSettings().setZoomControlsEnabled(false);
105                         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(40.8,-104.7),8));
106
107                         mapLoaded = true;
108                 }
109         }
110
111         public void update_ui(AltosState state) {
112                 if (state.from_pad != null) {
113                         mDistanceView.setText(String.format("%6.0f m", state.from_pad.distance));
114                         mBearingView.setText(String.format("%3.0f°", state.from_pad.bearing));
115                 }
116                 mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
117                 mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
118
119                 if (mapLoaded) {
120                 }
121         }
122
123 }