I am currently developing a crime map with Java, and want to have custom markers depending on whether a crime is closed or an open case. I've added if statements to change the marker picture depending on the crime status. However, they are not currently being assigned to the markers the way I want them to be.
I should also add that for some reason, i'm recieving the alert that says Variable 'crimeStatus' is never used, even though I am using it in this code.
public class CrimeData {
private String location;
private String crimeType;
private String crimeDetails;
private String crimeStatus;
private double latitude;
private double longitude;
// Add getters and setters for the above fields
public CrimeData(String location, String crimeType, String crimeDetails) {
this.location = location;
this.crimeType = crimeType;
this.crimeDetails = crimeDetails;
this.crimeStatus = crimeStatus;
}
public String getLocation() {
return location;
}
public String getCrimeType() {
return crimeType;
}
public String getCrimeDetails() {
return crimeDetails;
}
public String getCrimeStatus(){
return crimeStatus;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
crimeDataList = new ArrayList<>();
// Populate crimeDataList with data from the table
// TODO: Fetch data from the table and populate the crimeDataList
// Set up GeoApiContext with your Google Maps Geocoding API key
geoApiContext = new GeoApiContext.Builder()
.apiKey("XXXXXXXXXXX")
.build();
// Fetch data from the table and populate the crimeDataList
// Example data
String[][] tableData = {
{"XXXXXXX", "Criminal Trespass", "Occurred From 6/14/23, 1:43AM to 6/14/23, 1:43AM", "Closed"},
{"XXXXXXXXX", "Theft", "Occurred From 6/13/23, 10:00AM to 6/13/23, 11:00AM", "Open"},
{"XXXXXXX", "Assault", "Occurred From 6/12/23, 8:00PM to 6/12/23, 9:00PM", "Closed"},
};
for (String[] rowData : tableData) {
String location = rowData[0];
String crimeType = rowData[1];
String crimeDetails = rowData[2];
String crimeStatus = rowData[3];
// Create a new CrimeData object and add it to the list
CrimeData crimeData = new CrimeData(location, crimeType, crimeDetails);
crimeDataList.add(crimeData);
}
}
private BitmapDescriptor BitmapFromVector(Context applicationContext, int baseline_warning_24) {
// below line is use to generate a drawable.
Drawable vectorDrawable = ContextCompat.getDrawable(applicationContext, baseline_warning_24);
// below line is use to set bounds to our vector
// drawable.
vectorDrawable.setBounds(
0, 0, vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight());
// below line is use to create a bitmap for our
// drawable which we have added.
Bitmap bitmap = Bitmap.createBitmap(
vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
// below line is use to add bitmap in our canvas.
Canvas canvas = new Canvas(bitmap);
// below line is use to draw our
// vector drawable in canvas.
vectorDrawable.draw(canvas);
// after generating our bitmap we are returning our
// bitmap.
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setBuildingsEnabled(true);
map.setIndoorEnabled(true);
map.setTrafficEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
map.getUiSettings().setCompassEnabled(true);
map.getUiSettings().setMyLocationButtonEnabled(true);
try {
boolean success = map.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle));
if (!success) {
// Handle map style loading failure
}
} catch (Resources.NotFoundException e) {
// Handle exception if the JSON file is not found
}
// Set the custom InfoWindowAdapter
googleMap.setInfoWindowAdapter(this);
// Create a custom BitmapDescriptor from your PNG image
BitmapDescriptor customMarkerIcon = BitmapDescriptorFactory.fromResource(R.drawable.exclamation);
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(XXXXX, XXXXXX))
.title("Marker Title")
.snippet("Marker Snippet")
.icon(BitmapFromVector(getApplicationContext(), R.drawable.baseline_warning_24)));
// Iterate over the crime data and geocode each location
for (CrimeData crimeData : crimeDataList) {
String locationName = crimeData.getLocation();
if (crimeData.getCrimeStatus() == "Closed") {
customMarkerIcon = BitmapDescriptorFactory.fromResource(R.drawable.baseline_check_circle_24);
}
if (crimeData.getCrimeStatus() == "Open Case") {
customMarkerIcon = BitmapDescriptorFactory.fromResource(R.drawable.baseline_warning_24);
}
try {
// Perform geocoding request
GeocodingResult[] results = GeocodingApi.geocode(geoApiContext, locationName).await();
if (results != null && results.length > 0) {
// Extract the coordinates from the first result
double latitude = results[0].geometry.location.lat;
double longitude = results[0].geometry.location.lng;
// Create a LatLng object for the marker's position
LatLng markerPosition = new LatLng(latitude, longitude);
MarkerOptions markerOptions = new MarkerOptions()
.position(markerPosition)
.title(crimeData.getCrimeType())
.snippet(crimeData.getCrimeDetails())
.icon(customMarkerIcon);
// Add the marker to the map
map.addMarker(markerOptions);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Move the camera to the first marker position (if available)
if (crimeDataList.size() > 0) {
CrimeData firstCrimeData = crimeDataList.get(0);
LatLng firstMarkerPosition = new LatLng(firstCrimeData.getLatitude(), firstCrimeData.getLongitude());
map.moveCamera(CameraUpdateFactory.newLatLngZoom(firstMarkerPosition, 12f));
}
LatLng location = new LatLng(XXXX, XXXX);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(location)
.zoom(16) // Set your desired zoom level
.build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
What should I change to fix this?