altosdroid: Pop up menu of nearby trackers on map click
authorKeith Packard <keithp@keithp.com>
Sun, 12 Jul 2015 02:53:06 +0000 (19:53 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 12 Jul 2015 02:53:06 +0000 (19:53 -0700)
This lets the user select one of potentially many overlapping
trackers, and also makes it clear when the current tracker is being
changed.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java
altosdroid/src/org/altusmetrum/AltosDroid/AltosMapOffline.java
altosdroid/src/org/altusmetrum/AltosDroid/AltosMapOnline.java

index 3a5026e..4ac2405 100644 (file)
@@ -855,6 +855,22 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                update_state(null);
        }
 
+       void touch_trackers(Integer[] serials) {
+               AlertDialog.Builder builder_tracker = new AlertDialog.Builder(this);
+               builder_tracker.setTitle("Select Tracker");
+               final String[] trackers = new String[serials.length];
+               for (int i = 0; i < serials.length; i++)
+                       trackers[i] = String.format("%d", serials[i]);
+               builder_tracker.setItems(trackers,
+                                        new DialogInterface.OnClickListener() {
+                                                public void onClick(DialogInterface dialog, int item) {
+                                                        select_tracker(Integer.parseInt(trackers[item]));
+                                                }
+                                        });
+               AlertDialog alert_tracker = builder_tracker.create();
+               alert_tracker.show();
+       }
+
        void delete_track(int serial) {
                try {
                        mService.send(Message.obtain(null, TelemetryService.MSG_DELETE_SERIAL, (Integer) serial));
index 5240d61..0bf6ab2 100644 (file)
@@ -253,6 +253,8 @@ public class AltosMapOffline extends View implements ScaleGestureDetector.OnScal
        public void select_object(AltosLatLon latlon) {
                if (map.transform == null)
                        return;
+               ArrayList<Integer>      near = new ArrayList<Integer>();
+
                for (Rocket rocket : sorted_rockets()) {
                        if (rocket.position == null) {
                                debug("rocket %d has no position\n", rocket.serial);
@@ -262,10 +264,11 @@ public class AltosMapOffline extends View implements ScaleGestureDetector.OnScal
                        debug("check select %d distance %g width %d\n", rocket.serial, distance, rocket_bitmap.getWidth());
                        if (distance < rocket_bitmap.getWidth() * 2.0) {
                                debug("selecting %d\n", rocket.serial);
-                               altos_droid.select_tracker(rocket.serial);
-                               break;
+                               near.add(rocket.serial);
                        }
                }
+               if (near.size() != 0)
+                       altos_droid.touch_trackers(near.toArray(new Integer[0]));
        }
 
        class Line {
@@ -359,7 +362,6 @@ public class AltosMapOffline extends View implements ScaleGestureDetector.OnScal
 
        @Override
        protected void onDraw(Canvas view_canvas) {
-               debug("onDraw");
                if (map == null) {
                        debug("MapView draw without map\n");
                        return;
index 7691a13..3b56fb5 100644 (file)
@@ -21,15 +21,8 @@ import java.util.*;
 
 import org.altusmetrum.altoslib_7.*;
 
-import com.google.android.gms.maps.CameraUpdateFactory;
-import com.google.android.gms.maps.GoogleMap;
-import com.google.android.gms.maps.SupportMapFragment;
-import com.google.android.gms.maps.model.BitmapDescriptorFactory;
-import com.google.android.gms.maps.model.LatLng;
-import com.google.android.gms.maps.model.Marker;
-import com.google.android.gms.maps.model.MarkerOptions;
-import com.google.android.gms.maps.model.Polyline;
-import com.google.android.gms.maps.model.PolylineOptions;
+import com.google.android.gms.maps.*;
+import com.google.android.gms.maps.model.*;
 
 import android.app.Activity;
 import android.graphics.Color;
@@ -48,6 +41,7 @@ class RocketOnline implements Comparable {
        Marker          marker;
        int             serial;
        long            last_packet;
+       int             size;
 
        void set_position(AltosLatLon position, long last_packet) {
                marker.setPosition(new LatLng(position.lat, position.lon));
@@ -75,6 +69,8 @@ class RocketOnline implements Comparable {
                float x = bitmap.getWidth() / 2.0f - width / 2.0f;
                float y = bitmap.getHeight() / 2.0f - height / 2.0f;
 
+               size = bitmap.getWidth();
+
                canvas.drawText(text, 0, text.length(), x, y, paint);
                return bitmap;
        }
@@ -106,7 +102,7 @@ class RocketOnline implements Comparable {
        }
 }
 
-public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarkerClickListener {
+public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarkerClickListener, GoogleMap.OnMapClickListener {
        public SupportMapFragment mMapFragment;
        private GoogleMap mMap;
        private boolean mapLoaded = false;
@@ -152,14 +148,43 @@ public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarke
 //             getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
 //     }
 
-       public boolean onMarkerClick(Marker marker) {
-               for (RocketOnline rocket : rockets.values()) {
-                       if (rocket.marker.equals(marker)) {
-                               altos_droid.select_tracker(rocket.serial);
-                               return true;
-                       }
+       private double pixel_distance(LatLng a, LatLng b) {
+               Projection projection = mMap.getProjection();
+
+               Point   a_pt = projection.toScreenLocation(a);
+               Point   b_pt = projection.toScreenLocation(b);
+
+               return Math.hypot((double) (a_pt.x - b_pt.x), (double) (a_pt.y - b_pt.y));
+       }
+
+       private RocketOnline[] sorted_rockets() {
+               RocketOnline[]  rocket_array = rockets.values().toArray(new RocketOnline[0]);
+
+               Arrays.sort(rocket_array);
+               return rocket_array;
+       }
+
+       public void onMapClick(LatLng lat_lng) {
+               ArrayList<Integer>      near = new ArrayList<Integer>();
+
+               for (RocketOnline rocket : sorted_rockets()) {
+                       LatLng  pos = rocket.marker.getPosition();
+
+                       if (pos == null)
+                               continue;
+
+                       double distance = pixel_distance(lat_lng, pos);
+                       if (distance < rocket.size * 2)
+                               near.add(rocket.serial);
                }
-               return false;
+
+               if (near.size() != 0)
+                       altos_droid.touch_trackers(near.toArray(new Integer[0]));
+       }
+
+       public boolean onMarkerClick(Marker marker) {
+               onMapClick(marker.getPosition());
+               return true;
        }
 
        public void setupMap(int map_type) {
@@ -170,6 +195,7 @@ public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarke
                        mMap.getUiSettings().setTiltGesturesEnabled(false);
                        mMap.getUiSettings().setZoomControlsEnabled(false);
                        mMap.setOnMarkerClickListener(this);
+                       mMap.setOnMapClickListener(this);
 
                        mPadMarker = mMap.addMarker(
                                        new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))