1

I'm developing an android app where some users opens the same activity from their devices. There is a map in this activity and as users opens this activity from their devices, their location coordinates is fetched from Firebase and a marker based on these coordinates is shown on the map.

Here's my code:

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("currentLat").trim());
            cLngP.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 addMarkers() method:

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);
    }
}

Here's onLocationChanged():

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

The users keeps moving towards a specific location.

What I want is to move the respective markers to the new location as the users moves so that everybody can see where each and everyone currently is. Please help me figure it out.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133

3 Answers3

0

Save the result of addMarker and later call setPosition on it (result of addMarker).

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
danny117
  • 5,581
  • 1
  • 26
  • 35
0

It appears as if there is nothing wrong with your code. I am sorry I could not find anything wrong with it as it is, but here is a file that you can use as a guideline to find the problem:

http://pastebin.com/9qq484Fz

I hope you solve the problem soon! (Apologies for the messy code, it's pretty old)

Nathan Bird
  • 910
  • 1
  • 9
  • 23
  • thanks for the code, buddy. have you find the other way of doing this as you asked here: http://stackoverflow.com/q/39778805/6144372 – Hammad Nasir Feb 18 '17 at 19:28
  • and what is `mFriends`? – Hammad Nasir Feb 19 '17 at 01:24
  • @hammadnasir the friendlist and mFriends variables are just lists of friend IDs... Not crucial to the rest of the code. – Nathan Bird Feb 19 '17 at 20:45
  • `friendList` is being used in `onStart`. From where are you populating it? Please tell me, – Hammad Nasir Feb 21 '17 at 06:41
  • @HammadNasir I'm using friendList to store the Google Play Games Ids of my "friends" I populate it when someone adds a friend. I forgot to remove that from the Pastebin but because it was a guest account I can't change it. – Nathan Bird Feb 22 '17 at 15:07
0

1. Whenever onLocationChanged called means user moved to some distance.

Update user's updated location on Firebase in onLocationChanged so, other user can find your updated location.

Note: use geofire to update user location and see this answer.

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

2. call acceptingUserReference.child(requestID) code when you successfully update user location to Firebase

Community
  • 1
  • 1
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51