我试图显示在线商店和离线商店,但问题是它完美地显示了商店,但是当我单击“开/关”开关按钮时,标记将同时显示绿色和红色两种颜色
public void onMapReady(GoogleMap googleMap) {
map=googleMap;
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
}
map.setMyLocationEnabled(true);
databaseReference.orderByChild("action").equalTo("open").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot shops:dataSnapshot.getChildren() )
{
shoplocation shop=shops.getValue(shoplocation.class);
LatLng sydney = new LatLng(Double.parseDouble(shop.getLat()),Double.parseDouble(shop.getLon()));
map.addMarker(new MarkerOptions().position(sydney).title(shop.getShopname()).icon(BitmapDescriptorFactory.defaultMarker(
BitmapDescriptorFactory.HUE_GREEN
)));
map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
databaseReference.orderByChild("action").equalTo("close").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot shops:dataSnapshot.getChildren() )
{
shoplocation shop=shops.getValue(shoplocation.class);
LatLng sydney = new LatLng(Double.parseDouble(shop.getLat()),Double.parseDouble(shop.getLon()));
map.addMarker(new MarkerOptions().position(sydney).title(shop.getShopname()).icon(BitmapDescriptorFactory.defaultMarker(
BitmapDescriptorFactory.HUE_RED
)));
map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
这是开/关商店的一面 The part where shops are on/off
答案 0 :(得分:0)
在您的ValueEventListener
中,您总是添加新标记。但是切勿删除已添加的内容。
因此,当数据更改时,您可以在已添加的标记上方添加新的标记。
在添加红色和绿色标记之前,请先删除所有标记,然后才能开始制作ValueEventListener
。
类似(未经测试)的东西
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
googleMap.clear()
for(DataSnapshot shops:dataSnapshot.getChildren() )
{
shoplocation shop=shops.getValue(shoplocation.class);
LatLng sydney = new LatLng(...);
map.addMarker(...);
map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}