Advertisement
kitlolz012

MainActivity6(Map)

Nov 17th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 KB | Source Code | 0 0
  1. Main Activity 6 Java Code :
  2.  
  3. package com.example.gypsy;
  4.  
  5. import androidx.appcompat.app.AppCompatActivity;
  6. import androidx.appcompat.app.AlertDialog;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.net.Uri;
  10. import android.widget.LinearLayout;
  11. import android.widget.Toast;
  12. import com.google.android.material.button.MaterialButton;
  13. import com.google.firebase.database.DataSnapshot;
  14. import com.google.firebase.database.DatabaseError;
  15. import com.google.firebase.database.DatabaseReference;
  16. import com.google.firebase.database.FirebaseDatabase;
  17. import com.google.firebase.database.ValueEventListener;
  18.  
  19.  
  20. import android.os.Bundle;
  21.  
  22. public class MainActivity6 extends AppCompatActivity {
  23.  
  24.     @Override
  25.     protected void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.activity_main6);
  28.  
  29.         Intent intent = getIntent();
  30.         if (intent != null) {
  31.             String restoId = intent.getStringExtra("restoId");
  32.  
  33.             if (restoId != null) {
  34.                 LinearLayout linearLayout = findViewById(R.id.linearLayout);
  35.                 queryRestoDetails(restoId, linearLayout);
  36.             } else {
  37.                 // Handle the case where hotelId is null
  38.                 Toast.makeText(MainActivity6.this, "resto ID is null", Toast.LENGTH_SHORT).show();
  39.             }
  40.         }
  41.     }
  42.     private void queryRestoDetails(String hotelId, LinearLayout linearLayout) {
  43.         DatabaseReference restoRef = FirebaseDatabase.getInstance().getReference().child("restaurants").child(hotelId);
  44.         restoRef.addListenerForSingleValueEvent(new ValueEventListener() {
  45.             @Override
  46.             public void onDataChange(DataSnapshot dataSnapshot) {
  47.                 if (dataSnapshot.exists()) {
  48.                     String RestoName = dataSnapshot.child("name").getValue(String.class);
  49.                     double latitude = dataSnapshot.child("latitude").getValue(Double.class);
  50.                     double longitude = dataSnapshot.child("longitude").getValue(Double.class);
  51.  
  52.                     // Create a button with the hotel name
  53.                     createButtonForHotel(linearLayout, RestoName, latitude, longitude);
  54.                 }
  55.             }
  56.  
  57.             @Override
  58.             public void onCancelled(DatabaseError databaseError) {
  59.                 // Handle the database error
  60.                 Toast.makeText(MainActivity6.this, "Database error: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
  61.             }
  62.         });
  63.     }
  64.  
  65.     private void createButtonForHotel(LinearLayout linearLayout, String hotelName, double latitude, double longitude) {
  66.         // Create a new MaterialButton for the hotel
  67.         MaterialButton directionBtn = new MaterialButton(MainActivity6.this);
  68.         directionBtn.setText("Navigate to " + hotelName);
  69.  
  70.         // Set click listener for the button
  71.         directionBtn.setOnClickListener(v -> {
  72.             // Show an alert dialog indicating that the map coordinates may not be accurate
  73.             showAlertDialog(hotelName, latitude, longitude);
  74.         });
  75.  
  76.         // Add the button to the LinearLayout
  77.         linearLayout.addView(directionBtn);
  78.     }
  79.  
  80.     private void showAlertDialog(String hotelName, double latitude, double longitude) {
  81.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  82.         builder.setTitle("Map Coordinates Not Accurate");
  83.         builder.setMessage("The map coordinates for " + hotelName + " may not be accurate. Please consider manually searching for the location if its in correct .");
  84.  
  85.         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  86.             @Override
  87.             public void onClick(DialogInterface dialog, int which) {
  88.                 // Proceed with navigating to the location on the map
  89.                 directionFromCurrentMap(latitude, longitude);
  90.             }
  91.         });
  92.  
  93.         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  94.             @Override
  95.             public void onClick(DialogInterface dialog, int which) {
  96.                 // Cancel the navigation
  97.                 dialog.dismiss();
  98.             }
  99.         });
  100.  
  101.         // Show the alert dialog
  102.         builder.show();
  103.     }
  104.  
  105.     private void directionFromCurrentMap(double destinationLatitude, double destinationLongitude) {
  106.         Uri mapUri = Uri.parse("https://maps.google.com/maps?daddr=" + destinationLatitude + "," + destinationLongitude);
  107.         Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
  108.         startActivity(intent);
  109.     }
  110. }
  111.  
  112.  
  113. XML :
  114.  
  115. <?xml version="1.0" encoding="utf-8"?>
  116. <LinearLayout
  117.     xmlns:android="http://schemas.android.com/apk/res/android"
  118.     xmlns:tools="http://schemas.android.com/tools"
  119.     xmlns:app="http://schemas.android.com/apk/res-auto"
  120.     android:id="@+id/linearLayout"
  121.     android:layout_width="match_parent"
  122.     android:layout_height="match_parent"
  123.     android:orientation="vertical"
  124.     android:gravity="center"
  125.     android:padding="10dp"
  126.     tools:context=".MainActivity6">
  127.  
  128. </LinearLayout>
  129.  
Tags: Code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement