问题
I’m trying to display all riders’ position in google maps using geo-fire within 1 km radius. But display only one rider marker icon in google maps. Though I get all riders latitude and longitude.
Screenshot:
https://imgur.com/p0wUxRB.jpg
Code:
@Override
public void onLocationChanged(Location location) {
if (location != null) {
updateRiderPosition(new GeoLocation(location.getLatitude(), location.getLongitude()));
}
}
public void updateRiderPosition(GeoLocation location) {
ArrayList<Marker> markerList = new ArrayList<>();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(mPositionNode).child(mAuthId);
GeoFire geoFire = new GeoFire(ref);
GeoQuery geoQuery = geoFire.queryAtLocation(location, 1);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
//---------------------------------------------------
if (markerList != null) {
for (Marker marker : markerList) {
marker.remove();
}
}
markerList.add(mMap.addMarker(new MarkerOptions().position(new LatLng(location.latitude, location.longitude)).icon(icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_motorbike)))));
//---------------------------------------------------
}
@Override
public void onKeyExited(String key) {}
@Override
public void onKeyMoved(String key, GeoLocation location) {}
@Override
public void onGeoQueryReady() {}
@Override
public void onGeoQueryError(DatabaseError error) {}
});
}
回答1:
in onKeyEntered before adding your new marker, remove all previous ones
@Override
public void onKeyEntered(String key, GeoLocation location) {
//---------------------------------------------------
// this if remove all previous markers
if (markerList != null) {
for (Marker marker : markerList) {
marker.remove();
}
}
markerList.add(mMap.addMarker(new MarkerOptions().position(new LatLng(location.latitude, location.longitude)).icon(icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_motorbike)))));
//---------------------------------------------------
}
回答2:
@Override
public void onLocationChanged(Location location) {
if (location != null) {
updateRiderPosition(new GeoLocation(location.getLatitude(), location.getLongitude()));
}
}
private ArrayList<Marker> markerList = new ArrayList<>();
private void updateRiderPosition(GeoLocation location) {
if (markerList != null) {
for (Marker marker : markerList) {
marker.remove();
}
}
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(mPositionNode).child(mAuthId);
GeoFire geoFire = new GeoFire(ref);
GeoQuery geoQuery = geoFire.queryAtLocation(location, 1);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
markerList.add(mMap.addMarker(new MarkerOptions().position(new LatLng(location.latitude, location.longitude)).icon(icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_motorbike)))));
}
@Override
public void onKeyExited(String key) {}
@Override
public void onKeyMoved(String key, GeoLocation location) {}
@Override
public void onGeoQueryReady() {}
@Override
public void onGeoQueryError(DatabaseError error) {}
});
}
来源:https://stackoverflow.com/questions/54833857/multiple-marker-icon-move-by-updating-geofire-location-in-google-maps-for-androi