Search

How to Get Longitude and Latitude using Firebase with Android

    public void Get_Latitude_Longitude()
    {
        try {
            DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("maps1/" + MID);
            final ArrayList<String> a12 = new ArrayList<>();

            Integer Cnt = 0;
            reference.addValueEventListener( new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        maxid = (dataSnapshot.getChildrenCount());
                        Log.d("srinivas","Hai");
                        if (snapshot.getKey().toString().equals("latitude")) {
                            a12.add(snapshot.getValue().toString());
                            Log.d("srinivas",""+snapshot.getValue().toString());
                        }
                        if (snapshot.getKey().toString().equals("longitude")) {
                            a12.add(snapshot.getValue().toString());
                            Log.d("srinivas",""+snapshot.getValue().toString());
                        }

                        if (snapshot.getKey().toString().equals("speed")) {
                            s_speed = Double.parseDouble(snapshot.getValue().toString());
                            calspeed = (s_speed * 18) / 5;
                            int ab = (int) Math.round(calspeed);
                            mspeed.setText(ab + "Kmph");
                            if (!b_no_entry) {
                                if (ab >= 1) {
                                    j_status = "Running";
                                    ///////////b_no_entry = false;
                                    new DB_Online().execute("");
                                    Stoptimer();
                                } else {
                                        j_status = "Idle";
                                        START_TIME_IN_MILLIS = 120000;
                                        startTimer_idle_running();
                                }
                            }
                            
                        }
                    }

                    T_Latitude = Double.parseDouble(a12.get(0).toString());
                    T_Longitude = Double.parseDouble(a12.get(1).toString());
                    Log.d("latitude_logitude",+T_Latitude+T_Longitude+"get the latlong");

                    
                    String a1 = onLocationChanged1(T_Latitude, T_Longitude);
                    Log.d("latlang","geolatitude");

                    T_Address = a1.toString();
                    Geo_Address = a1.toString();
                    Log.d("address","Address: "+Geo_Address+", Destination: "+T_Dest);
                    if (Geo_Address.toString().trim().length()>=10) {
                        String T_dest5 = T_Dest.replace(" ","");
                        String T_dest6 = T_dest5.replace(",","");
                        String Geo1 = Geo_Address.toString().replace(",","");
                        String Geo2 = Geo_Address.toString().replace(" ","");
                        if (Geo2.toString().toUpperCase().contains(T_dest6)){
                            Log.d("destination show","currect");
                            dreached.setVisibility(View.VISIBLE);

                            Log.d("Sheela", "Destination Reached");
                        } else {

                        }
                    }

                     if (T_Lon.toString().length()>=4 && T_Lat.toString().length()>=4) {
                        Double J_Lon = Double.parseDouble(T_Lon);
                        Double J_Lat = Double.parseDouble(T_Lat);
                        if ((T_Longitude>=J_Lon-0.0040) && (T_Longitude<=J_Lon+0.0040)) {
                            if ((T_Latitude >= (J_Lat - 0.0040)) && (T_Latitude <= (J_Lat + 0.0040))) {
                                    Toast.makeText(MapsActivity.this, "latitude" + J_Lat + "longitude" + J_Lon + "", Toast.LENGTH_SHORT).show();
                                    dreached.setVisibility(View.VISIBLE);
                            }
                        }

                         if ((T_Latitude>=(J_Lat-0.0040)) && (T_Latitude<=(J_Lat+0.0040))) {
                             if ((T_Longitude >= (J_Lon - 0.0040)) && (T_Longitude <= (J_Lon + 0.0040))) {
                                 Toast.makeText(MapsActivity.this, "latitude" + J_Lat + "longitude" + J_Lon + "", Toast.LENGTH_SHORT).show();
                                 dreached.setVisibility(View.VISIBLE);
                             }
                         }
                     }




                    mDatabase.child("Stat/" + MID + "/currentstatus").setValue(j_status);
                    mDatabase.child("Stat/" + MID + "/dateandtime").setValue(getDateTime2021());
                    mDatabase.child("Stat/" + MID + "/address").setValue(a1.toString());
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });

            if (j_status=="Idle" || j_status.equals("Idle"))
            {
            }
            else if (j_status=="Running" || j_status.equals("Running"))
            {


            }

            else
            {
                new DB_Online().execute("");
            }
        }
        catch (Exception ex)
        {
            Toast.makeText(this, "Error: "+ex.toString(), Toast.LENGTH_SHORT).show();
        }
    }

8 views0 comments

Recent Posts

See All

//Step 1: Create the following function to upload the file public async void uploadFile() { var stream = File.Open(@"d:\sankar.png", FileMode.Open); var auth = new FirebaseAuthProvider(new Firebase.Au