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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. marker.setTag(clusterItem);
  71. super.onClusterItemRendered(clusterItem, marker);
  72. }
  73. public StopDboMapItem getStopItem(String id)
  74. {
  75. return _items.get(id);
  76. }
  77. }
  78. @BindView(R.id.mapView)
  79. MapView _mapView;
  80. private OnStopClicked _onStopSelectedConsumer;
  81. private GoogleMap _map;
  82. private List<StopsDbo> _stopsDbos;
  83. private StopDboMapRenderer _renderer;
  84. @Override
  85. public void onStart() {
  86. _mapView.onStart();
  87. super.onStart();
  88. }
  89. @Override
  90. public void onResume() {
  91. _mapView.onResume();
  92. super.onResume();
  93. }
  94. @Override
  95. public void onPause() {
  96. _mapView.onPause();
  97. super.onPause();
  98. }
  99. @Override
  100. public void onStop() {
  101. _mapView.onStop();
  102. super.onStop();
  103. }
  104. @Override
  105. public void onDestroy() {
  106. _mapView.onDestroy();
  107. super.onDestroy();
  108. }
  109. @Override
  110. public void onSaveInstanceState(Bundle outState) {
  111. _mapView.onSaveInstanceState(outState);
  112. super.onSaveInstanceState(outState);
  113. }
  114. @Override
  115. public void onLowMemory() {
  116. _mapView.onLowMemory();
  117. super.onLowMemory();
  118. }
  119. @Nullable
  120. @Override
  121. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  122. View v = inflater.inflate(R.layout.fragment_stop_picker_map, container, false);
  123. ButterKnife.bind(this, v);
  124. _mapView.onCreate(savedInstanceState);
  125. _mapView.getMapAsync(new OnMapReadyCallback() {
  126. @Override
  127. public void onMapReady(GoogleMap googleMap) {
  128. _map = googleMap;
  129. setupMap();
  130. }
  131. });
  132. loadStops();
  133. return v;
  134. }
  135. @Override
  136. public void onAttach(Context context) {
  137. super.onAttach(context);
  138. if (context instanceof OnStopClicked) {
  139. _onStopSelectedConsumer = (OnStopClicked) context;
  140. }
  141. }
  142. @Override
  143. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  144. if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
  145. ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  146. _map.setMyLocationEnabled(true);
  147. }
  148. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  149. }
  150. public void setupMap()
  151. {
  152. _map.getUiSettings().setMyLocationButtonEnabled(true);
  153. _map.getUiSettings().setCompassEnabled(true);
  154. _map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.3786565, -71.3354953), 9));
  155. _map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
  156. @Override
  157. public boolean onMarkerClick(Marker marker) {
  158. if (marker.getTag() instanceof StopDboMapItem) {
  159. Toast.makeText(getContext(), R.string.stop_add_map_help, Toast.LENGTH_LONG).show();
  160. }
  161. return false;
  162. }
  163. });
  164. _map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
  165. @Override
  166. public void onInfoWindowClick(Marker marker) {
  167. onStopSelected(_renderer.getStopItem(marker.getId()).getStopsDbo());
  168. }
  169. });
  170. if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
  171. ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  172. _map.setMyLocationEnabled(true);
  173. }
  174. else {
  175. requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
  176. }
  177. if (_stopsDbos != null) {
  178. addStops();
  179. }
  180. }
  181. public void loadStops()
  182. {
  183. final ProgressDialog progressDialog = new ProgressDialog(getContext());
  184. progressDialog.setIndeterminate(true);
  185. progressDialog.setCancelable(false);
  186. progressDialog.show();
  187. progressDialog.setTitle(R.string.loading);
  188. progressDialog.setMessage(getString(R.string.loading_stops));
  189. StopsBusiness.getAll(STSBusiness.getConfig(getContext()))
  190. .then(new LuConsumer<List<StopsDbo>>() {
  191. @Override
  192. public void execute(List<StopsDbo> data) {
  193. progressDialog.dismiss();
  194. _stopsDbos = data;
  195. if (_map != null) {
  196. addStops();
  197. }
  198. }
  199. }, new LuConsumer<LuPromiseError>() {
  200. @Override
  201. public void execute(LuPromiseError data) {
  202. progressDialog.dismiss();
  203. _stopsDbos = null;
  204. Toast.makeText(getContext(), data.getError(), Toast.LENGTH_LONG).show();
  205. }
  206. });
  207. }
  208. public void addStops()
  209. {
  210. ClusterManager<StopDboMapItem> clusterManager = new ClusterManager<>(getContext(), _map);
  211. _renderer = new StopDboMapRenderer(getContext(), _map, clusterManager);
  212. clusterManager.setRenderer(_renderer);
  213. for (StopsDbo stopsDbo : _stopsDbos) {
  214. clusterManager.addItem(new StopDboMapItem(stopsDbo));
  215. }
  216. _map.setOnCameraIdleListener(clusterManager);
  217. _map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.3786565, -71.3354953), 9));
  218. }
  219. public void onStopSelected(StopsDbo stopsDbo)
  220. {
  221. if (_onStopSelectedConsumer != null) {
  222. _onStopSelectedConsumer.onStopSlicker(stopsDbo);
  223. }
  224. }
  225. }