通过在Google Maps for Android中更新GeoFire位置来移动多个标记图标

问题描述 投票:0回答:2

我试图在1公里范围内使用地理火力显示所有车手在谷歌地图中的位置。但是在谷歌地图中只显示一个骑手标记图标。虽然我得到了所有车手的纬度和经度。

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

}
android google-maps geofire
2个回答
0
投票

在添加新标记之前的onKeyEntered中,删除所有以前的标记

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

0
投票
@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) {}
    });

}
© www.soinside.com 2019 - 2024. All rights reserved.