Advertisement
kitlolz012

DynamicHotelMap

Nov 21st, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. package com.example.gypsy;
  2.  
  3. import androidx.appcompat.app.AlertDialog;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.widget.Toast;
  10. import com.google.firebase.database.DataSnapshot;
  11. import com.google.firebase.database.DatabaseError;
  12. import com.google.firebase.database.DatabaseReference;
  13. import com.google.firebase.database.FirebaseDatabase;
  14. import com.google.firebase.database.ValueEventListener;
  15.  
  16. public class DynamicHotelMap extends AppCompatActivity {
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_dynamic_hotel_map);
  22.  
  23.         Intent intent = getIntent();
  24.         if (intent != null) {
  25.             String hotelId = intent.getStringExtra("hotelId");
  26.  
  27.             if (hotelId != null) {
  28.                 queryHotelDetails(hotelId);
  29.             } else {
  30.                 // Handle the case where hotelId is null
  31.                 Toast.makeText(DynamicHotelMap.this, "Hotel ID is null", Toast.LENGTH_SHORT).show();
  32.             }
  33.         }
  34.     }
  35.  
  36.     private void queryHotelDetails(String hotelId) {
  37.         DatabaseReference hotelRef = FirebaseDatabase.getInstance().getReference().child("hotels").child(hotelId);
  38.         hotelRef.addListenerForSingleValueEvent(new ValueEventListener() {
  39.             @Override
  40.             public void onDataChange(DataSnapshot dataSnapshot) {
  41.                 if (dataSnapshot.exists()) {
  42.                     String hotelName = dataSnapshot.child("name").getValue(String.class);
  43.                     double latitude = dataSnapshot.child("latitude").getValue(Double.class);
  44.                     double longitude = dataSnapshot.child("longitude").getValue(Double.class);
  45.  
  46.                     // Show an alert dialog indicating that the map coordinates may not be accurate
  47.                     showAlertDialog(hotelName, latitude, longitude);
  48.                 }
  49.             }
  50.  
  51.             @Override
  52.             public void onCancelled(DatabaseError databaseError) {
  53.                 // Handle the database error
  54.                 Toast.makeText(DynamicHotelMap.this, "Database error: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
  55.             }
  56.         });
  57.     }
  58.  
  59.     private void showAlertDialog(String hotelName, double latitude, double longitude) {
  60.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  61.         builder.setTitle("PLEASE TAKE NOTE");
  62.         builder.setMessage("The map coordinates for " + hotelName + " may not be accurate. Please consider manually searching for the location if it's incorrect.");
  63.  
  64.         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  65.             @Override
  66.             public void onClick(DialogInterface dialog, int which) {
  67.                 // Proceed with navigating to the location on the map
  68.                 directionFromCurrentMap(latitude, longitude);
  69.             }
  70.         });
  71.  
  72.         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  73.             @Override
  74.             public void onClick(DialogInterface dialog, int which) {
  75.                 // Cancel the navigation
  76.                 dialog.dismiss();
  77.                 finish(); // Close the activity if the user cancels
  78.             }
  79.         });
  80.  
  81.         // Show the alert dialog
  82.         builder.show();
  83.     }
  84.  
  85.     private void directionFromCurrentMap(double destinationLatitude, double destinationLongitude) {
  86.         Uri mapUri = Uri.parse("https://maps.google.com/maps?daddr=" + destinationLatitude + "," + destinationLongitude);
  87.         Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
  88.         startActivity(intent);
  89.         finish(); // Close the activity after starting the map intent
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement