@Override public void onInfoWindowClick(Marker marker) { /* tell main activity to show takeoff details */ Pair<Marker, Takeoff> pair = markers.get(marker.getId()); if (pair != null) { Takeoff takeoff = pair.second; Bundle args = new Bundle(); args.putParcelable(TakeoffDetails.ARG_TAKEOFF, takeoff); ((FlyWithMeActivity) getActivity()) .showFragment("takeoffDetails," + takeoff.getId(), TakeoffDetails.class, args); } else { Log.w(getClass().getName(), "Strange, could not find takeoff for marker"); } }
@Override protected Runnable doInBackground(CameraPosition... cameraPositions) { try { final Map<Takeoff, MarkerOptions> addMarkers = new HashMap<>(); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity()); if (sharedPref.getBoolean("pref_map_show_takeoffs", true)) { LatLng latLng = cameraPositions[0].target; Location mapLocation = new Location(LocationManager.PASSIVE_PROVIDER); mapLocation.setLatitude(latLng.latitude); mapLocation.setLongitude(latLng.longitude); /* get the 25 nearest takeoffs */ List<Takeoff> takeoffs = Database.getTakeoffs( getActivity(), latLng.latitude, latLng.longitude, 25, false, true); /* add markers */ for (Takeoff takeoff : takeoffs) { Bitmap bitmap = Bitmap.createBitmap( markerBitmap.getWidth(), markerBitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); if (!takeoff.isFavourite()) paint.setAlpha(123); canvas.drawBitmap(markerBitmap, 0, 0, paint); if (takeoff.hasNorthExit()) canvas.drawBitmap(markerNorthBitmap, 0, 0, paint); if (takeoff.hasNortheastExit()) canvas.drawBitmap(markerNortheastBitmap, 0, 0, paint); if (takeoff.hasEastExit()) canvas.drawBitmap(markerEastBitmap, 0, 0, paint); if (takeoff.hasSoutheastExit()) canvas.drawBitmap(markerSoutheastBitmap, 0, 0, paint); if (takeoff.hasSouthExit()) canvas.drawBitmap(markerSouthBitmap, 0, 0, paint); if (takeoff.hasSouthwestExit()) canvas.drawBitmap(markerSouthwestBitmap, 0, 0, paint); if (takeoff.hasWestExit()) canvas.drawBitmap(markerWestBitmap, 0, 0, paint); if (takeoff.hasNorthwestExit()) canvas.drawBitmap(markerNorthwestBitmap, 0, 0, paint); if (takeoff.getPilotsToday() > 0) canvas.drawBitmap(markerExclamation, 0, 0, paint); else if (takeoff.getPilotsLater() > 0) canvas.drawBitmap(markerExclamationYellow, 0, 0, paint); String snippet = getString(R.string.height) + ": " + takeoff.getHeight() + "m\n" + getString(R.string.distance) + ": " + (int) mapLocation.distanceTo(takeoff.getLocation()) / 1000 + "km"; MarkerOptions markerOptions = new MarkerOptions() .position( new LatLng( takeoff.getLocation().getLatitude(), takeoff.getLocation().getLongitude())) .title(takeoff.getName()) .snippet(snippet) .icon(BitmapDescriptorFactory.fromBitmap(bitmap)) .anchor(0.5f, 0.875f); addMarkers.put(takeoff, markerOptions); } } /* remove markers that we didn't "add" */ return new Runnable() { @Override public void run() { // remove markers that no longer should be visible for (Iterator<Map.Entry<String, Pair<Marker, Takeoff>>> iterator = markers.entrySet().iterator(); iterator.hasNext(); ) { Map.Entry<String, Pair<Marker, Takeoff>> entry = iterator.next(); if (!addMarkers.containsKey(entry.getValue().second)) { entry.getValue().first.remove(); iterator.remove(); } } // add markers that should be visible for (Map.Entry<Takeoff, MarkerOptions> addEntry : addMarkers.entrySet()) { boolean alreadyShown = false; for (Map.Entry<String, Pair<Marker, Takeoff>> entry : markers.entrySet()) { if (addEntry.getKey().equals(entry.getValue().second)) { alreadyShown = true; break; } } if (!alreadyShown) { Marker marker = map.addMarker(addEntry.getValue()); Pair<Marker, Takeoff> pair = new Pair<>(marker, addEntry.getKey()); markers.put(marker.getId(), pair); } } } }; } catch (Exception e) { Log.w(getClass().getName(), "doInBackground() failed unexpectedly", e); } return null; }