Skip to content

Instantly share code, notes, and snippets.

@communikein
Last active January 16, 2018 14:30
Show Gist options
  • Save communikein/13ae17d296e29a95d67ca90a8926c3f6 to your computer and use it in GitHub Desktop.
Save communikein/13ae17d296e29a95d67ca90a8926c3f6 to your computer and use it in GitHub Desktop.
public class PoisMapFragment extends Fragment implements OnMapReadyCallback {
private AcFragmentPoisMapBinding mBinding;
/* Might be null if Google Play services APK is not available. */
private GoogleMap mMap = null;
private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";
private static final LatLng bergamo = new LatLng(45.6983, 9.6773);
private static final LatLng rome = new LatLng(41.9028, 12.4964);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/* Inflate the layout for this fragment */
mBinding = DataBindingUtil.inflate(inflater, R.layout.ac_fragment_pois_map, container, false);
initMap(savedInstanceState);
return mBinding.getRoot();
}
public void setViewModel(PoisViewModel viewModel) {
this.mViewModel = viewModel;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (getParentViewModel() != null) {
getParentViewModel().getObservableAllPois().observe(this, list -> {
if (list != null) {
Log.d(LOG_TAG, "New data received. Size: " + list.size());
updateMap(list);
}
});
}
}
private PoisViewModel getParentViewModel() {
if (getActivity() != null)
return ((MainActivity) getActivity()).getViewModel();
else
return null;
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.mMap = googleMap;
if (getParentViewModel() != null) {
updateMap(getParentViewModel().getObservableAllPois().getValue());
}
}
private void initMap(Bundle savedInstanceState) {
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
}
mBinding.map.onCreate(mapViewBundle);
mBinding.map.getMapAsync(this);
}
private void updateMap(List<Poi> pois) {
if (mMap != null) {
for (Poi poi : pois) {
LatLng coords = new LatLng(poi.getLocationLat(), poi.getLocationLng());
mMap.addMarker(new MarkerOptions().position(coords));
}
CameraPosition.Builder builder = new CameraPosition.Builder();
builder.target(rome);
builder.zoom(5f);
CameraUpdate update = CameraUpdateFactory.newCameraPosition(builder.build());
mMap.moveCamera(update);
}
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Bundle mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY);
if (mapViewBundle == null) {
mapViewBundle = new Bundle();
outState.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle);
}
mBinding.map.onSaveInstanceState(mapViewBundle);
}
@Override
public void onResume() {
mBinding.map.onResume();
super.onResume();
}
@Override
public void onPause() {
mBinding.map.onPause();
super.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mBinding.map.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mBinding.map.onLowMemory();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment