You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StopMapPickerFragment.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package com.rthoni.stssaguenay.ui.fragments;
  2. import android.Manifest;
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.content.pm.PackageManager;
  6. import android.os.Bundle;
  7. import android.support.annotation.NonNull;
  8. import android.support.annotation.Nullable;
  9. import android.support.v4.app.ActivityCompat;
  10. import android.support.v4.app.Fragment;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.Toast;
  15. import com.google.android.gms.maps.CameraUpdateFactory;
  16. import com.google.android.gms.maps.GoogleMap;
  17. import com.google.android.gms.maps.MapView;
  18. import com.google.android.gms.maps.OnMapReadyCallback;
  19. import com.google.android.gms.maps.model.LatLng;
  20. import com.google.android.gms.maps.model.Marker;
  21. import com.google.android.gms.maps.model.MarkerOptions;
  22. import com.google.maps.android.clustering.ClusterItem;
  23. import com.google.maps.android.clustering.ClusterManager;
  24. import com.google.maps.android.clustering.view.DefaultClusterRenderer;
  25. import com.luticate.utils.business.LuConsumer;
  26. import com.luticate.utils.business.LuPromise;
  27. import com.luticate.utils.dbo.LuPromiseError;
  28. import com.rthoni.stssaguenay.R;
  29. import com.rthoni.stssaguenay.business.STSBusiness;
  30. import com.rthoni.stssaguenay.business.StopsBusiness;
  31. import com.rthoni.stssaguenay.dbo.StopsDbo;
  32. import com.rthoni.stssaguenay.ui.activities.StopPickerActivity;
  33. import com.rthoni.stssaguenay.ui.interfaces.OnStopClicked;
  34. import java.util.HashMap;
  35. import java.util.List;
  36. import butterknife.BindView;
  37. import butterknife.ButterKnife;
  38. /**
  39. * Created by robin on 9/29/16.
  40. */
  41. public class StopMapPickerFragment extends Fragment {
  42. public static class StopDboMapItem implements ClusterItem {
  43. private LatLng _latLng;
  44. private StopsDbo _stopsDbo;
  45. StopDboMapItem(StopsDbo stopsDbo) {
  46. _stopsDbo = stopsDbo;
  47. _latLng = new LatLng(_stopsDbo.getPosY(), _stopsDbo.getPosX());
  48. }
  49. @Override
  50. public LatLng getPosition() {
  51. return _latLng;
  52. }
  53. public StopsDbo getStopsDbo() {
  54. return _stopsDbo;
  55. }
  56. }
  57. public static class StopDboMapRenderer extends DefaultClusterRenderer<StopDboMapItem> {
  58. private HashMap<String, StopDboMapItem> _items = new HashMap<>();
  59. public StopDboMapRenderer(Context context, GoogleMap map, ClusterManager<StopDboMapItem> clusterManager) {
  60. super(context, map, clusterManager);
  61. }
  62. @Override
  63. protected void onBeforeClusterItemRendered(StopDboMapItem item, MarkerOptions markerOptions) {
  64. markerOptions.title(item.getStopsDbo().getFullName());
  65. super.onBeforeClusterItemRendered(item, markerOptions);
  66. }
  67. @Override
  68. protected void onClusterItemRendered(StopDboMapItem clusterItem, Marker marker) {
  69. _items.put(marker.getId(), clusterItem);
  70. super.onClusterItemRendered(clusterItem, marker);
  71. }
  72. public StopDboMapItem getStopItem(String id)
  73. {
  74. return _items.get(id);
  75. }
  76. }
  77. @BindView(R.id.mapView)
  78. MapView _mapView;
  79. private OnStopClicked _onStopSelectedConsumer;
  80. private GoogleMap _map;
  81. private List<StopsDbo> _stopsDbos;
  82. private StopDboMapRenderer _renderer;
  83. @Override
  84. public void onStart() {
  85. _mapView.onStart();
  86. super.onStart();
  87. }
  88. @Override
  89. public void onResume() {
  90. _mapView.onResume();
  91. super.onResume();
  92. }
  93. @Override
  94. public void onPause() {
  95. _mapView.onPause();
  96. super.onPause();
  97. }
  98. @Override
  99. public void onStop() {
  100. _mapView.onStop();
  101. super.onStop();
  102. }
  103. @Override
  104. public void onDestroy() {
  105. _mapView.onDestroy();
  106. super.onDestroy();
  107. }
  108. @Override
  109. public void onSaveInstanceState(Bundle outState) {
  110. _mapView.onSaveInstanceState(outState);
  111. super.onSaveInstanceState(outState);
  112. }
  113. @Override
  114. public void onLowMemory() {
  115. _mapView.onLowMemory();
  116. super.onLowMemory();
  117. }
  118. @Nullable
  119. @Override
  120. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  121. View v = inflater.inflate(R.layout.fragment_stop_picker_map, container, false);
  122. ButterKnife.bind(this, v);
  123. _mapView.onCreate(savedInstanceState);
  124. _mapView.getMapAsync(new OnMapReadyCallback() {
  125. @Override
  126. public void onMapReady(GoogleMap googleMap) {
  127. _map = googleMap;
  128. setupMap();
  129. }
  130. });
  131. loadStops();
  132. return v;
  133. }
  134. @Override
  135. public void onAttach(Context context) {
  136. super.onAttach(context);
  137. if (context instanceof OnStopClicked) {
  138. _onStopSelectedConsumer = (OnStopClicked) context;
  139. }
  140. }
  141. @Override
  142. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  143. if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
  144. ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  145. _map.setMyLocationEnabled(true);
  146. }
  147. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  148. }
  149. public void setupMap()
  150. {
  151. _map.getUiSettings().setMyLocationButtonEnabled(true);
  152. _map.getUiSettings().setCompassEnabled(true);
  153. _map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.3786565, -71.3354953), 9));
  154. _map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
  155. @Override
  156. public boolean onMarkerClick(Marker marker) {
  157. Toast.makeText(getContext(), R.string.stop_add_map_help, Toast.LENGTH_LONG).show();
  158. return false;
  159. }
  160. });
  161. _map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
  162. @Override
  163. public void onInfoWindowClick(Marker marker) {
  164. onStopSelected(_renderer.getStopItem(marker.getId()).getStopsDbo());
  165. }
  166. });
  167. if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
  168. ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  169. _map.setMyLocationEnabled(true);
  170. }
  171. else {
  172. requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
  173. }
  174. if (_stopsDbos != null) {
  175. addStops();
  176. }
  177. }
  178. public void loadStops()
  179. {
  180. final ProgressDialog progressDialog = new ProgressDialog(getContext());
  181. progressDialog.setIndeterminate(true);
  182. progressDialog.setCancelable(false);
  183. progressDialog.show();
  184. progressDialog.setTitle(R.string.loading);
  185. progressDialog.setMessage(getString(R.string.loading_stops));
  186. StopsBusiness.getAll(STSBusiness.getConfig(getContext()))
  187. .then(new LuConsumer<List<StopsDbo>>() {
  188. @Override
  189. public void execute(List<StopsDbo> data) {
  190. progressDialog.dismiss();
  191. _stopsDbos = data;
  192. if (_map != null) {
  193. addStops();
  194. }
  195. }
  196. }, new LuConsumer<LuPromiseError>() {
  197. @Override
  198. public void execute(LuPromiseError data) {
  199. progressDialog.dismiss();
  200. _stopsDbos = null;
  201. Toast.makeText(getContext(), data.getError(), Toast.LENGTH_LONG).show();
  202. }
  203. });
  204. }
  205. public void addStops()
  206. {
  207. ClusterManager<StopDboMapItem> clusterManager = new ClusterManager<>(getContext(), _map);
  208. _renderer = new StopDboMapRenderer(getContext(), _map, clusterManager);
  209. clusterManager.setRenderer(_renderer);
  210. for (StopsDbo stopsDbo : _stopsDbos) {
  211. clusterManager.addItem(new StopDboMapItem(stopsDbo));
  212. }
  213. _map.setOnCameraIdleListener(clusterManager);
  214. _map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.3786565, -71.3354953), 9));
  215. }
  216. public void onStopSelected(StopsDbo stopsDbo)
  217. {
  218. if (_onStopSelectedConsumer != null) {
  219. _onStopSelectedConsumer.onStopSlicker(stopsDbo);
  220. }
  221. }
  222. }