Advertisement
kitlolz012

MainActivity5(Map)

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