Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.gypsy;
- import androidx.appcompat.app.AlertDialog;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.widget.Toast;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- public class DynamicHotelMap extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_dynamic_hotel_map);
- Intent intent = getIntent();
- if (intent != null) {
- String hotelId = intent.getStringExtra("hotelId");
- if (hotelId != null) {
- queryHotelDetails(hotelId);
- } else {
- // Handle the case where hotelId is null
- Toast.makeText(DynamicHotelMap.this, "Hotel ID is null", Toast.LENGTH_SHORT).show();
- }
- }
- }
- private void queryHotelDetails(String hotelId) {
- DatabaseReference hotelRef = FirebaseDatabase.getInstance().getReference().child("hotels").child(hotelId);
- hotelRef.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- if (dataSnapshot.exists()) {
- String hotelName = dataSnapshot.child("name").getValue(String.class);
- double latitude = dataSnapshot.child("latitude").getValue(Double.class);
- double longitude = dataSnapshot.child("longitude").getValue(Double.class);
- // Show an alert dialog indicating that the map coordinates may not be accurate
- showAlertDialog(hotelName, latitude, longitude);
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- // Handle the database error
- Toast.makeText(DynamicHotelMap.this, "Database error: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
- }
- });
- }
- private void showAlertDialog(String hotelName, double latitude, double longitude) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("PLEASE TAKE NOTE");
- builder.setMessage("The map coordinates for " + hotelName + " may not be accurate. Please consider manually searching for the location if it's incorrect.");
- builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // Proceed with navigating to the location on the map
- directionFromCurrentMap(latitude, longitude);
- }
- });
- builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // Cancel the navigation
- dialog.dismiss();
- finish(); // Close the activity if the user cancels
- }
- });
- // Show the alert dialog
- builder.show();
- }
- private void directionFromCurrentMap(double destinationLatitude, double destinationLongitude) {
- Uri mapUri = Uri.parse("https://maps.google.com/maps?daddr=" + destinationLatitude + "," + destinationLongitude);
- Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
- startActivity(intent);
- finish(); // Close the activity after starting the map intent
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement