我使用 android studio 应用程序。有 3 个用户(管理员、父母、司机)他们每个人都必须看到一张带有不同标记的地图,但是!!当我打开父母和司机活动时,我只看到管理标记! 我尝试在打开地图之前清理标记,但它不起作用
AdminMapFragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.admin_map_fragment, container, false);
// Get a reference to the SearchView
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.ADMIN_MAP);
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
adminMap = googleMap;
MarkerOptions school = new MarkerOptions()
.position(new LatLng(23.6045298658, 58.4443127667))
.title("School");
MarkerOptions bus1 = new MarkerOptions()
.position(new LatLng(23.62949, 58.22531))
.title("X128");
MarkerOptions bus2 = new MarkerOptions()
.position(new LatLng(23.650098, 58.196164))
.title("A211");
MarkerOptions bus3 = new MarkerOptions()
.position(new LatLng(23.625267, 58.245206))
.title("Q123");
MarkerOptions bus4 = new MarkerOptions()
.position(new LatLng(23.613507, 58.253369))
.title("W575");
adminMap.addMarker(school);
adminMap.addMarker(bus1);
adminMap.addMarker(bus2);
adminMap.addMarker(bus3);
adminMap.addMarker(bus4);
// Wait for layout to occur
View mapView = supportMapFragment.getView();
if (mapView != null && mapView.getViewTreeObserver() != null) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Remove the listener to avoid continuously calling this method
mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(new LatLng(23.6045298658, 58.4443127667));
builder.include(new LatLng(23.62949, 58.22531));
builder.include(new LatLng(23.650098, 58.196164));
builder.include(new LatLng(23.625267, 58.245206));
builder.include(new LatLng(23.613507, 58.253369));
LatLngBounds bounds = builder.build();
int padding = 100; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
adminMap.animateCamera(cu);
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng latLng) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(latLng.latitude + "KG" + latLng.longitude);
googleMap.clear();
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
googleMap.addMarker(markerOptions);
}
});
}
});
}
}
});
return view;
}
ParentMapFragment:
public class ParentMapFragment extends Fragment {
private GoogleMap parentMap;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@NonNull Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_parent_map, container, false);
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.parent_map);
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
parentMap = googleMap;
LatLng location1 = new LatLng(23.6045298658, 58.4443127667);
parentMap.addMarker(new MarkerOptions()
.position(location1)
.title("school"));
LatLng location2 = new LatLng(23.642816, 58.211161);
parentMap.addMarker(new MarkerOptions()
.position(location2)
.title("Q123"));
PolylineOptions options = new PolylineOptions()
.add(location1)
.add(location2)
.color(Color.RED);
parentMap.addPolyline(options);
// Wait for layout to occur
View mapView = supportMapFragment.getView();
if (mapView != null && mapView.getViewTreeObserver() != null) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Remove the listener to avoid continuously calling this method
mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(new LatLng(23.6045298658, 58.4443127667));
builder.include(new LatLng(23.642816, 58.211161));
LatLngBounds bounds = builder.build();
int padding = 100; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
parentMap.animateCamera(cu);
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng latLng) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(latLng.latitude + "KG" + latLng.longitude);
googleMap.clear();
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
googleMap.addMarker(markerOptions);
}
});
}
});
}
}
});
return view;
}
}