Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a custom Map marker for the Case List #2935

Merged
merged 4 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 53 additions & 15 deletions app/src/org/commcare/gis/EntityMapActivity.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.commcare.gis;

import static org.commcare.views.EntityView.FORM_IMAGE;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Pair;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
Expand All @@ -20,8 +25,11 @@
import org.commcare.activities.EntityDetailActivity;
import org.commcare.cases.entity.Entity;
import org.commcare.dalvik.R;
import org.commcare.preferences.HiddenPreferences;
import org.commcare.suite.model.Detail;
import org.commcare.suite.model.DetailField;
import org.commcare.suite.model.EntityDatum;
import org.commcare.utils.MediaUtil;
import org.commcare.utils.SerializationUtil;
import org.commcare.views.UserfacingErrorHandling;
import org.javarosa.core.model.data.GeoPointData;
Expand All @@ -39,12 +47,16 @@
public class EntityMapActivity extends CommCareActivity implements OnMapReadyCallback,
GoogleMap.OnInfoWindowClickListener {
private static final int MAP_PADDING = 50; // Number of pixels to pad bounding region of markers
private static final int DEFAULT_MARKER_SIZE = 120;

private final Vector<Pair<Entity<TreeReference>, LatLng>> entityLocations = new Vector<>();
private final HashMap<Marker, TreeReference> markerReferences = new HashMap<>();

private GoogleMap mMap;

// keeps track of detail field index that should be used to show a custom icon
private int imageFieldIndex = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -71,6 +83,7 @@ private void addEntityData() {
if (selectDatum != null) {
Detail detail = CommCareApplication.instance().getCurrentSession()
.getDetail(selectDatum.getShortDetail());
evalImageFieldIndex(detail);
for (Entity<TreeReference> entity : EntityMapUtils.getEntities(detail, selectDatum.getNodeset())) {
for (int i = 0; i < detail.getHeaderForms().length; ++i) {
GeoPointData data = EntityMapUtils.getEntityLocation(entity, detail, i);
Expand All @@ -83,54 +96,79 @@ private void addEntityData() {
}
}

private void evalImageFieldIndex(Detail detail) {
DetailField[] fields = detail.getFields();
for (int i = 0; i < fields.length; i++) {
if (fields[i].getTemplateForm().equals(FORM_IMAGE)) {
imageFieldIndex = i;
break;
}
}
}

@Override
public void onMapReady(final GoogleMap map) {
mMap = map;

if (entityLocations.size() > 0) {
boolean showCustomMapMarker = HiddenPreferences.shouldShowCustomMapMarker();
LatLngBounds.Builder builder = new LatLngBounds.Builder();
// Add markers to map and find bounding region
for (Pair<Entity<TreeReference>, LatLng> entityLocation : entityLocations) {
Marker marker = mMap.addMarker(new MarkerOptions()
MarkerOptions markerOptions = new MarkerOptions()
.position(entityLocation.second)
.title(entityLocation.first.getFieldString(0))
.snippet(entityLocation.first.getFieldString(1)));
.snippet(entityLocation.first.getFieldString(1));
if (showCustomMapMarker) {
markerOptions.icon(getEntityIcon(entityLocation.first));
}
Marker marker = mMap.addMarker(markerOptions);
markerReferences.put(marker, entityLocation.first.getElement());
builder.include(entityLocation.second);
}
final LatLngBounds bounds = builder.build();

// Move camera to be include all markers
mMap.setOnMapLoadedCallback(() -> mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, MAP_PADDING)));
mMap.setOnMapLoadedCallback(
() -> mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, MAP_PADDING)));
}

mMap.setOnInfoWindowClickListener(this);
setMapLocationEnabled(true);
}

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
private BitmapDescriptor getEntityIcon(Entity<TreeReference> entity) {
if (imageFieldIndex != -1) {
String jrUri = String.valueOf(entity.getData()[imageFieldIndex]);
Bitmap bitmap = MediaUtil.inflateDisplayImage(this, jrUri, DEFAULT_MARKER_SIZE, DEFAULT_MARKER_SIZE,
true);
if (bitmap != null) {
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
}
return null;
}

@Override
protected void onResume() {
super.onResume();

if (mMap != null && (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
mMap.setMyLocationEnabled(true);
}
setMapLocationEnabled(true);
shubham1g5 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
protected void onPause() {
super.onPause();
setMapLocationEnabled(false);
}

private void setMapLocationEnabled(boolean enabled) {
if (mMap != null) {
mMap.setOnMapLoadedCallback(null); // Avoid memory leak in callback
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(false);
boolean fineLocationPermission = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
boolean coarseLocationPermission = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
if (fineLocationPermission || coarseLocationPermission) {
mMap.setMyLocationEnabled(enabled);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions app/src/org/commcare/preferences/HiddenPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public class HiddenPreferences {
public final static String DONT_SHOW_PENDING_SYNC_DIALOG = "dont-show-pending-sync-dialog";
private static final String ENABLE_BACKGROUND_SYNC = "cc-enable-background-sync";

private static final String ENABLE_CUSTOM_MAP_MARKER = "cc-enable-custom-map-marker";

/**
* The domain name in the application profile file comes in the <domain>.commcarehq.org form,
* this is standard across the different HQ servers. This constant is to store that suffix and
Expand Down Expand Up @@ -435,6 +437,11 @@ public static boolean shouldShowUnsentFormsWhenZero() {
return properties.getString(SHOW_UNSENT_FORMS_WHEN_ZERO, PrefValues.NO).equals(PrefValues.YES);
}

public static boolean shouldShowCustomMapMarker() {
SharedPreferences properties = CommCareApplication.instance().getCurrentApp().getAppPreferences();
return properties.getString(ENABLE_CUSTOM_MAP_MARKER, PrefValues.NO).equals(PrefValues.YES);
}


public static boolean preUpdateSyncNeeded() {
SharedPreferences properties = CommCareApplication.instance().getCurrentApp().getAppPreferences();
Expand Down
Loading