Advertisement
kitlolz012

DynamicResto Map

Nov 21st, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 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.  
  17. public class DynamicRestoMap extends AppCompatActivity {
  18.  
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_dynamic_resto_map);
  23.  
  24.         Intent intent = getIntent();
  25.         if (intent != null) {
  26.             String restoId = intent.getStringExtra("restoId");
  27.  
  28.             if (restoId != null) {
  29.                 queryRestoDetails(restoId);
  30.             } else {
  31.                 // Handle the case where hotelId is null
  32.                 Toast.makeText(DynamicRestoMap.this, "resto ID is null", Toast.LENGTH_SHORT).show();
  33.             }
  34.         }
  35.     }
  36.     private void queryRestoDetails(String restoId) {
  37.         DatabaseReference restoRef = FirebaseDatabase.getInstance().getReference().child("restaurants").child(restoId);
  38.         restoRef.addListenerForSingleValueEvent(new ValueEventListener() {
  39.             @Override
  40.             public void onDataChange(DataSnapshot dataSnapshot) {
  41.                 if (dataSnapshot.exists()) {
  42.                     String restoName = 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.                     // Create a button with the hotel name
  47.                     showAlertDialog(restoName, latitude, longitude);
  48.                 }
  49.             }
  50.  
  51.             @Override
  52.             public void onCancelled(DatabaseError databaseError) {
  53.                 // Handle the database error
  54.                 Toast.makeText(DynamicRestoMap.this, "Database error: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
  55.             }
  56.         });
  57.     }
  58.  
  59.     private void showAlertDialog(String restoName, 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 " + restoName + " may not be accurate. Please consider manually searching for the location if its in correct .");
  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.             }
  78.         });
  79.  
  80.         // Show the alert dialog
  81.         builder.show();
  82.     }
  83.  
  84.     private void directionFromCurrentMap(double destinationLatitude, double destinationLongitude) {
  85.         Uri mapUri = Uri.parse("https://maps.google.com/maps?daddr=" + destinationLatitude + "," + destinationLongitude);
  86.         Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
  87.         startActivity(intent);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement