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_1.*;
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.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;
44 public class TabMap extends Fragment implements AltosDroidTab {
45 AltosDroid mAltosDroid;
47 private SupportMapFragment mMapFragment;
48 private GoogleMap mMap;
49 private boolean mapLoaded = false;
51 private Marker mRocketMarker;
52 private Marker mPadMarker;
53 private Polyline mPolyline;
55 private TextView mDistanceView;
56 private TextView mBearingView;
57 private TextView mLatitudeView;
58 private TextView mLongitudeView;
61 public void onAttach(Activity activity) {
62 super.onAttach(activity);
63 mAltosDroid = (AltosDroid) activity;
64 mAltosDroid.registerTab(this);
68 public void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
71 mMapFragment = new SupportMapFragment() {
73 public void onActivityCreated(Bundle savedInstanceState) {
74 super.onActivityCreated(savedInstanceState);
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);
92 public void onActivityCreated(Bundle savedInstanceState) {
93 super.onActivityCreated(savedInstanceState);
94 getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
98 public void onDestroyView() {
99 super.onDestroyView();
101 mAltosDroid.unregisterTab(this);
104 //Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
105 //FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
106 //ft.remove(fragment);
110 private void setupMap() {
111 mMap = mMapFragment.getMap();
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));
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))
125 mPadMarker = mMap.addMarker(
126 new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
127 .position(new LatLng(0,0))
131 mPolyline = mMap.addPolyline(
132 new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
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));
147 mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
148 mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
151 mRocketMarker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
152 mRocketMarker.setVisible(true);
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);
157 if (state.state == AltosLib.ao_flight_pad) {
158 mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
159 mPadMarker.setVisible(true);