Unable to add markers using latitude and longitude retrieved from Firebase and stored in ArrayList<String>

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 04:57:11

问题


I am developing an app which has an activity having map in it. When users open this activity from their devices, their current location coordinates is saved in the Firebase and then are retrieved instantly and a marker is shown on their current location. This procedure is happening successfully.

Here's how I'm retrieving the latitude and longitude from firebase:

acceptingUserReference.child(requestID).child(key).addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {

                                    if (dataSnapshot.getValue() != null) {
                                            final Map<String, String> newAcceptedUser = (Map<String, String>) dataSnapshot.getValue();

                                            nameOfP.add(newAcceptedUser.get("pName"));                      
                                            cLatP.add(newAcceptedUser.get("cLat").trim());
                                            cLngP.add(newAcceptedUser.get("cLng").trim());

                                            currentLat.add(newAcceptedUser.get("currentLat").trim());
                                            currentLng.add(newAcceptedUser.get("currentLng").trim());

                                            addMarkers();

                                            //Check map is loaded
                                            mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                                                @Override
                                                public void onMapLoaded() {
                                                    mMap.getUiSettings().setZoomControlsEnabled(true);
                                                    mMap.getUiSettings().setMapToolbarEnabled(true);
                                                    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                                                    mMap.setMaxZoomPreference(19.0f);  mMap.setMyLocationEnabled(true);

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

                                }
                            });

Here's how:

public void addMarkers() {
        mMap.clear();
        venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng)));
        markersList.add(venueMarker);
        for (int i = 0; i < nameOfP.size(); i++) {
            p = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(cLatP.get(i)), Double.valueOf(cLngP.get(i)))).title(nameOfP.get(i).trim()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
            markersList.add(pMarker);
        }
}

The problem arises when I the users moves and their location changes. Now, I want to remove the marker from their previous location and show it on their updated current location. For doing this, I'm using LocationRequest to get location and then saving the updated location in Firebase.

Here's how:

    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;
        currentLtAU = mCurrentLocation.getLatitude();
        currentLnAU = mCurrentLocation.getLongitude();

        aReference.child(rID).child(userID).child("currentLat").setValue(String.valueOf(currentLtAU));
        aReference.child(rID).child(userID).child("currentLng").setValue(String.valueOf(currentLnAU));    
        updateMarkers();

}

Now, to update markers based on this updated location, I'm trying this:

public void updateMarkers() {
        mMap.clear();
        venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng)));
    markersList.add(venueMarker);
    for (int i = 0; i < nameOfP.size(); i++) {
        p = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(currentLat.get(i)), Double.valueOf(currentLng.get(i)))).title(nameOfP.get(i).trim()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
        markersList.add(pMarker);
    }
}

but on doing this, many markers are getting added and also the app is getting crashed giving this error: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 and also addMarkers() gets called everytime.

Please help me to figure out what is going wrong here and how to achieve what I want.

来源:https://stackoverflow.com/questions/42432191/unable-to-add-markers-using-latitude-and-longitude-retrieved-from-firebase-and-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!