-1

I understand that this question is consider as similar qiestion, but, I really can't solved and i am just newbie trying to learn android.

So, the expecting outcome was having an activity which is google map to allow user itself to look at their SavedPlace. So my cloud firestore structure look like this now:

cloud firestore structure

Then, in my code, what i did so far is:

    public class FoodShareActivity extends AppCompatActivity {
    @BindView(R.id.toolbar)
    Toolbar mtoolbar;
    SupportMapFragment supportMapFragment;
    FusedLocationProviderClient client;
    private ChildEventListener mChildEventListener;
    private DatabaseReference mDataRef;
    Marker marker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_share);
        ButterKnife.bind(this);


        setSupportActionBar(mtoolbar);
        getSupportActionBar().setTitle("FoodShare");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_baseline_keyboard_backspace_24);

        supportMapFragment = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.google_map);
        client = LocationServices.getFusedLocationProviderClient(this);
        if (ActivityCompat.checkSelfPermission(FoodShareActivity.this,
                Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            getCurrentLocation();

        } else {
            ActivityCompat.requestPermissions(FoodShareActivity.this, new String[]{
                            Manifest.permission.ACCESS_FINE_LOCATION,}, 44
            );
        }

        ChildEventListener mChildEventListener;
//        mDataRef.push().setValue(marker);
    }

    private void getCurrentLocation() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        Task<Location> task = client.getLastLocation();
        task.addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if(location != null){
                    supportMapFragment.getMapAsync(new OnMapReadyCallback() {
                        @Override
                        public void onMapReady(GoogleMap googleMap) {
                                    LatLng latLng = new LatLng(location.getLatitude(),
                                            location.getLongitude());
                                    MarkerOptions options = new MarkerOptions().position(latLng).title("Im hrer");
                                    googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                                    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,17.0f));
                                    googleMap.addMarker(options);
                            FirebaseDatabase database = FirebaseDatabase.getInstance();
                        DatabaseReference myRef = database.getReference("SavedPlace");
                        myRef.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                                    placeModel post = postSnapshot.getValue(placeModel.class);
                                    MarkerOptions markerOptions = new MarkerOptions();
                                    double lat = Double.parseDouble(String.valueOf(post.getLat()));
                                    double lng = Double.parseDouble(String.valueOf(post.getLong()));

                                    LatLng latLng = new LatLng(lat, lng);
                                    markerOptions.position(latLng);
                                    markerOptions.title(post.getPlaceName());
                                    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));

                                    googleMap.addMarker(markerOptions);
                                    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                                    googleMap.animateCamera(CameraUpdateFactory.zoomTo(12));
                                }
                            }

                            @Override
                            public void onCancelled(DatabaseError error) { }
                        });


                                }
                            });
                        }
            }
        });
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return false;
    }
}

For my placeModel,

public class placeModel {
    private String placeAddress;
    private String placeDetails;
    private String placeLatLong;
    private String placeName;
    private String placeNote;
    double Lat;
    double Long;

    public placeModel(){
        //empty constuctor needed
    }

    public placeModel(String placeAddress, String placeDetails, String placeLatLong, String placeName, String placeNote, double Lat, double Long) {
        this.placeAddress = placeAddress;
        this.placeDetails = placeDetails;
        this.placeLatLong = placeLatLong;

        this.placeName = placeName;
        this.placeNote = placeNote;
        this.Lat = Lat;
        this.Long = Long;

    }


    public String getPlaceAddress() {
        return placeAddress;
    }
    public String getPlaceName() { return placeName; }
    public double getLat() {
        Log.d("ga", "checkAA"+ Lat);

        return Lat;
    }
    public double getLong() { return Long; }


    public String getDetails() {
        return placeDetails;
    }
    public String getLatLong() { return placeLatLong; }
    public String getNote() { return placeNote; }

}

The map acitivty works fine, and able to see my current location. But the marker for location from database doennt show.

Current outcome

The final outcome that is to have multiple marker on google map marking based on latitude and longitude from firebase.

PLEASE HELP ME, I STUCK HERE FOR ABOUT 2 DAYS.

cococrunch
  • 15
  • 7
  • `However, the latitude and longitude from firebase does not work` could you explain ? what is it doing ? what isn't it doing ? – a_local_nobody Nov 06 '20 at 10:06
  • Sorry for that, So, what i mean actually is it doesnt show any marker except the current location. What i want actually is to have all the location in firebase – cococrunch Nov 06 '20 at 10:11
  • You will not be able to use `lat` and `long` if you don't change the names in the database to start with a lower letter, as well in your class. You can also use an annotation, as explained in the [duplicate](https://stackoverflow.com/questions/54069574/firebase-android-listview-not-being-displayed). – Alex Mamo Nov 06 '20 at 11:10
  • I've changed it, still the same, just nothing happen over there. I've also tried to use Log.d to debug, it wont run Log in this page. – cococrunch Nov 06 '20 at 11:42

1 Answers1

0

I have done the same thing in android So customise my code and it may help you.

FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("Dams");
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    Dam post = postSnapshot.getValue(Dam.class);
                    damsList.add(post);
                    
                    MarkerOptions markerOptions = new MarkerOptions();
                    double lat = Double.parseDouble(String.valueOf(post.getDamLat()));
                    double lng = Double.parseDouble(String.valueOf(post.getDamLong()));

                    LatLng latLng = new LatLng(lat, lng);
                    markerOptions.position(latLng);
                    markerOptions.title(post.getDamLevel());
                    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));

                    mMap.addMarker(markerOptions);
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                    mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
                }

                pd.dismiss();
            }

            @Override
            public void onCancelled(DatabaseError error) {
                pd.dismiss();

            }
        });
Imad khan
  • 289
  • 4
  • 6
  • Hey there, Thank you so much for that. May I know whats damsList.add(post); actually means. Anyway, thanks ya. – cococrunch Nov 06 '20 at 10:17
  • You can remove it. It is a list of posts that i am retrieving from the fire-base – Imad khan Nov 06 '20 at 10:21
  • Alright, after i customise your version, the marker still doenst came out. I've updated the latest code above my question. Could you please help me to check it, Please. Sorry for wasting your time. – cococrunch Nov 06 '20 at 10:34
  • Check out if data is coming from the fire-base – Imad khan Nov 06 '20 at 10:56