I have the following values taken from the previous activity to display in the following code below:
Scenario:
Emergency Type:
Specific location markers, if any
and the location of the selected user off a listview
private GoogleMap mMap;
private double latitude;
private double longitude;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 99;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emergency_details);
// Receive data from intent extras
String scenarioData = getIntent().getStringExtra("Scenario");
String emergencyTypeData = getIntent().getStringExtra("Emergency Type");
latitude = getIntent().getDoubleExtra("Latitude", 0.0);
longitude = getIntent().getDoubleExtra("Longitude", 0.0);
// Find TextViews for scenario data and emergency type
TextView tvScenarioData = findViewById(R.id.tvScenario);
TextView tvEmergencyTypeData = findViewById(R.id.tvEmergencyType);
// Set text for scenario data and emergency type TextViews
tvScenarioData.setText(scenarioData);
tvEmergencyTypeData.setText(emergencyTypeData);
// Check and request location permission if needed
checkLocationPermission();
// Initialize SupportMapFragment and handle map customization
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.miniMapFragment);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
} else {
Toast.makeText(this, "Error loading map!", Toast.LENGTH_SHORT).show();
}
Log.d("EmergencyDetails", "Received Latitude: " + latitude);
Log.d("EmergencyDetails", "Received Longitude: " + longitude);
}
private void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// Permission already granted
// Proceed with map initialization or other location-dependent tasks
initializeMap();
} else {
// Request location permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
}
}
private void initializeMap() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (mMap != null) {
try {
// Add marker to the map if latitude and longitude are valid
LatLng location = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(location));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15f));
mMap.setMyLocationEnabled(true);
} catch (SecurityException e) {
Log.e("MapError", "Error enabling location on map: " + e.getMessage());
}
} else {
Toast.makeText(this, "Map is not ready yet", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Location permission not granted", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Location permission granted, initialize the map
initializeMap();
} else {
// Location permission denied, handle accordingly
Toast.makeText(this, "Location permission denied", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// Permission already granted, initialize the map
initializeMap();
} else {
// Request location permission if not granted
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
}
}
}
The location data is being retrieved from Firebase through a previous activity via Intents, however, when on the current activity, it does not display the location retrieved. Please do note, that the Maps feature is set up correctly for this project, as it is already being used in other activities but is working properly.
Here is the logcat:
09:20:47.949 D Received Latitude: 120.7152931
09:20:47.949 D Received Longitude: 15.4487153