Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main Activity 6 Java Code :
- package com.example.gypsy;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.appcompat.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.Uri;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- import com.google.android.material.button.MaterialButton;
- 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;
- import android.os.Bundle;
- public class MainActivity6 extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main6);
- Intent intent = getIntent();
- if (intent != null) {
- String restoId = intent.getStringExtra("restoId");
- if (restoId != null) {
- LinearLayout linearLayout = findViewById(R.id.linearLayout);
- queryRestoDetails(restoId, linearLayout);
- } else {
- // Handle the case where hotelId is null
- Toast.makeText(MainActivity6.this, "resto ID is null", Toast.LENGTH_SHORT).show();
- }
- }
- }
- private void queryRestoDetails(String hotelId, LinearLayout linearLayout) {
- DatabaseReference restoRef = FirebaseDatabase.getInstance().getReference().child("restaurants").child(hotelId);
- restoRef.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- if (dataSnapshot.exists()) {
- String RestoName = dataSnapshot.child("name").getValue(String.class);
- double latitude = dataSnapshot.child("latitude").getValue(Double.class);
- double longitude = dataSnapshot.child("longitude").getValue(Double.class);
- // Create a button with the hotel name
- createButtonForHotel(linearLayout, RestoName, latitude, longitude);
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- // Handle the database error
- Toast.makeText(MainActivity6.this, "Database error: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
- }
- });
- }
- private void createButtonForHotel(LinearLayout linearLayout, String hotelName, double latitude, double longitude) {
- // Create a new MaterialButton for the hotel
- MaterialButton directionBtn = new MaterialButton(MainActivity6.this);
- directionBtn.setText("Navigate to " + hotelName);
- // Set click listener for the button
- directionBtn.setOnClickListener(v -> {
- // Show an alert dialog indicating that the map coordinates may not be accurate
- showAlertDialog(hotelName, latitude, longitude);
- });
- // Add the button to the LinearLayout
- linearLayout.addView(directionBtn);
- }
- private void showAlertDialog(String hotelName, double latitude, double longitude) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Map Coordinates Not Accurate");
- builder.setMessage("The map coordinates for " + hotelName + " may not be accurate. Please consider manually searching for the location if its in correct .");
- 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();
- }
- });
- // 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);
- }
- }
- XML :
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/linearLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center"
- android:padding="10dp"
- tools:context=".MainActivity6">
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement