Multiple Marker Icon Move by Updating GeoFire Location in Google Maps for Android

六眼飞鱼酱① 提交于 2020-01-06 07:57:09

问题


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

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