Skip to content

Commit

Permalink
Set custom map icon
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham1g5 committed Jan 13, 2025
1 parent 5eb9cb1 commit d543c9b
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion 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 @@ -21,7 +26,9 @@
import org.commcare.cases.entity.Entity;
import org.commcare.dalvik.R;
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 @@ -45,6 +52,9 @@ public class EntityMapActivity extends CommCareActivity implements OnMapReadyCal

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 +81,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,6 +94,16 @@ 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;
Expand All @@ -94,7 +115,8 @@ public void onMapReady(final GoogleMap map) {
Marker marker = mMap.addMarker(new MarkerOptions()
.position(entityLocation.second)
.title(entityLocation.first.getFieldString(0))
.snippet(entityLocation.first.getFieldString(1)));
.snippet(entityLocation.first.getFieldString(1))
.icon(getEntityIcon(entityLocation.first)));
markerReferences.put(marker, entityLocation.first.getElement());
builder.include(entityLocation.second);
}
Expand All @@ -109,6 +131,17 @@ public void onMapReady(final GoogleMap map) {
setMapLocationEnabled(true);
}

private BitmapDescriptor getEntityIcon(Entity<TreeReference> entity) {
if (imageFieldIndex != -1) {
String jrUri = String.valueOf(entity.getData()[imageFieldIndex]);
Bitmap bitmap = MediaUtil.inflateDisplayImage(this, jrUri, 120, 120, true);
if (bitmap != null) {
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
}
return null;
}

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

0 comments on commit d543c9b

Please sign in to comment.