Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

android - Move all the markers on the map to their updated/new current location periodically as users moves

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...