问题
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