Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.gypsy;
- import androidx.appcompat.app.AlertDialog;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.widget.Toast;
- 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 DynamicRestoMap extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_dynamic_resto_map);
- Intent intent = getIntent();
- if (intent != null) {
- String restoId = intent.getStringExtra("restoId");
- if (restoId != null) {
- queryRestoDetails(restoId);
- } else {
- // Handle the case where hotelId is null
- Toast.makeText(DynamicRestoMap.this, "resto ID is null", Toast.LENGTH_SHORT).show();
- }
- }
- }
- private void queryRestoDetails(String restoId) {
- DatabaseReference restoRef = FirebaseDatabase.getInstance().getReference().child("restaurants").child(restoId);
- 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
- showAlertDialog(restoName, latitude, longitude);
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- // Handle the database error
- Toast.makeText(DynamicRestoMap.this, "Database error: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
- }
- });
- }
- private void showAlertDialog(String restoName, double latitude, double longitude) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("PLEASE TAKE NOTE");
- builder.setMessage("The map coordinates for " + restoName + " 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement