Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.gypsy;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- 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;
- public class MainActivity5 extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main5);
- Intent intent = getIntent();
- if (intent != null) {
- String hotelId = intent.getStringExtra("hotelId");
- if (hotelId != null) {
- LinearLayout linearLayout = findViewById(R.id.linearLayout);
- queryHotelDetails(hotelId, linearLayout);
- } else {
- // Handle the case where hotelId is null
- Toast.makeText(MainActivity5.this, "Hotel ID is null", Toast.LENGTH_SHORT).show();
- }
- }
- }
- private void queryHotelDetails(String hotelId, LinearLayout linearLayout) {
- DatabaseReference hotelRef = FirebaseDatabase.getInstance().getReference().child("hotels").child(hotelId);
- hotelRef.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- if (dataSnapshot.exists()) {
- String hotelName = 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, hotelId, hotelName, latitude, longitude);
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- // Handle the database error
- Toast.makeText(MainActivity5.this, "Database error: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
- }
- });
- }
- private void createButtonForHotel(LinearLayout linearLayout, String hotelId, String hotelName, double latitude, double longitude) {
- // Create a new MaterialButton for the hotel
- MaterialButton directionBtn = new MaterialButton(MainActivity5.this);
- directionBtn.setText("Navigate to " + hotelName);
- // Set click listener for the button
- directionBtn.setOnClickListener(v -> {
- directionFromCurrentMap(latitude, longitude);
- });
- // Add the button to the LinearLayout
- linearLayout.addView(directionBtn);
- }
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement