Guest User

Untitled

a guest
Dec 10th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 KB | None | 0 0
  1. public class TrackingOrder extends FragmentActivity implements OnMapReadyCallback,
  2. GoogleApiClient.ConnectionCallbacks,
  3. GoogleApiClient.OnConnectionFailedListener,
  4. LocationListener{
  5.  
  6. private GoogleMap mMap;
  7.  
  8. private final static int PLAY_SERVICE_RESOLUTION_REQUEST = 1000;
  9. private final static int LOCATION_PERMISSION_REQUEST = 1001;
  10.  
  11. private Location mLastLocation;
  12. private GoogleApiClient mGoogleApiClient;
  13. private LocationRequest mLocationRequest;
  14. private LocationCallback mLocationCallback;
  15. private FusedLocationProviderClient mFusedLocationProviderClient;
  16.  
  17. private static int UPDATE_INTERVAL = 1000;
  18. private static int FATEST_INTERVAL = 5000;
  19. private static int DISPLACEMENT = 10;
  20.  
  21. private IGeoCoordinates mService;
  22.  
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_tracking_order);
  28.  
  29. mService = Common.getGeoCodeService();
  30.  
  31. if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  32. != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
  33. (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
  34.  
  35. {
  36. requestRuntimePermission();
  37. }
  38. else{
  39.  
  40. if(checkPlayServices()){
  41. buildGoogleApiClient();
  42. createLocationRequest();
  43. createLocationCallBack();
  44. }
  45. }
  46. mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
  47.  
  48. displayLocation();
  49.  
  50.  
  51.  
  52. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  53. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  54. .findFragmentById(R.id.map);
  55. mapFragment.getMapAsync(this);
  56. }
  57.  
  58. private void requestRuntimePermission() {
  59.  
  60. ActivityCompat.requestPermissions(this, new String[]
  61. {
  62. Manifest.permission.ACCESS_COARSE_LOCATION,
  63. Manifest.permission.ACCESS_FINE_LOCATION},
  64. LOCATION_PERMISSION_REQUEST);
  65. }
  66.  
  67. @Override
  68. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  69. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  70. switch (requestCode) {
  71.  
  72. case LOCATION_PERMISSION_REQUEST:
  73. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  74.  
  75. if (checkPlayServices()) {
  76.  
  77. buildGoogleApiClient();
  78. createLocationRequest();
  79.  
  80. displayLocation();
  81. }
  82. }
  83. break;
  84. }
  85. }
  86.  
  87. private void displayLocation() {
  88. if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  89. != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
  90. (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
  91.  
  92. {
  93. requestRuntimePermission();
  94. }
  95. else
  96. {
  97. mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
  98. if (mLastLocation != null)
  99. {
  100. double latitude = mLastLocation.getLatitude();
  101. double longitude = mLastLocation.getLongitude();
  102.  
  103. //Add Marker in location and move the camera
  104.  
  105. LatLng yourLocation = new LatLng(latitude, longitude);
  106. mMap.addMarker(new MarkerOptions().position(yourLocation).title("Your Location"));
  107. mMap.moveCamera(CameraUpdateFactory.newLatLng(yourLocation));
  108. mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
  109.  
  110. //After add Marker for location, Add Marker for Order and draw route
  111.  
  112. drawRoute(yourLocation, Common.currentRequest.getAddress());
  113. }
  114. else
  115. {
  116. //Toast.makeText(this, "Couldn't get the location", Toast.LENGTH_SHORT).show();
  117. Log.d("DEBUG", "Couldn't get the location" );
  118. }
  119. }
  120.  
  121. }
  122.  
  123. private void drawRoute(final LatLng yourLocation, String address) {
  124.  
  125. mService.getGeoCode(address).enqueue(new Callback<String>() {
  126. @Override
  127. public void onResponse(Call<String> call, Response<String> response) {
  128. try{
  129. JSONObject jsonObject = new JSONObject(response.body().toString());
  130.  
  131. String lat = ((JSONArray)jsonObject.get("results"))
  132. .getJSONObject(0)
  133. .getJSONObject("geometry")
  134. .getJSONObject("location")
  135. .get("lat").toString();
  136.  
  137. String lng = ((JSONArray)jsonObject.get("results"))
  138. .getJSONObject(0)
  139. .getJSONObject("geometry")
  140. .getJSONObject("location")
  141. .get("lng").toString();
  142.  
  143. LatLng orderLocation = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
  144.  
  145. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.deliverybox);
  146. bitmap = Common.scaleBitmap(bitmap, 70, 70);
  147.  
  148. MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
  149. .title("Order of " + Common.currentRequest.getPhone())
  150. .position(orderLocation);
  151.  
  152. mMap.addMarker(marker);
  153.  
  154. //draw route
  155.  
  156. mService.getDirections(yourLocation.latitude + "," + yourLocation.longitude,
  157. orderLocation.latitude + "," + orderLocation.longitude)
  158. .enqueue(new Callback<String>() {
  159. @Override
  160. public void onResponse(Call<String> call, Response<String> response) {
  161.  
  162. new ParserTask().execute(response.body().toString());
  163. }
  164.  
  165. @Override
  166. public void onFailure(Call<String> call, Throwable t) {
  167.  
  168. }
  169. });
  170.  
  171. }
  172. catch (JSONException e){
  173.  
  174. e.printStackTrace();
  175. }
  176. }
  177.  
  178. @Override
  179. public void onFailure(Call<String> call, Throwable t) {
  180.  
  181. }
  182. });
  183. }
  184.  
  185.  
  186. private void createLocationRequest() {
  187.  
  188. mLocationRequest = new LocationRequest();
  189. mLocationRequest.setInterval(UPDATE_INTERVAL);
  190. mLocationRequest.setFastestInterval(FATEST_INTERVAL);
  191. mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  192. mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
  193. }
  194.  
  195. private void createLocationCallBack() {
  196.  
  197. mLocationCallback = new LocationCallback(){
  198. @Override
  199. public void onLocationResult(LocationResult locationResult) {
  200. super.onLocationResult(locationResult);
  201. mLastLocation = locationResult.getLastLocation();
  202. mLocationRequest = LocationRequest.create();
  203. mGoogleApiClient.connect();
  204. onLocationChanged(mLastLocation);
  205.  
  206. }
  207. };
  208. }
  209.  
  210. protected synchronized void buildGoogleApiClient() {
  211.  
  212. mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
  213.  
  214. mGoogleApiClient = new GoogleApiClient.Builder(this)
  215. .addConnectionCallbacks(this)
  216. .addOnConnectionFailedListener(this)
  217. .addApi(LocationServices.API)
  218. .build();
  219.  
  220. mGoogleApiClient.connect();
  221.  
  222. }
  223.  
  224. private boolean checkPlayServices() {
  225.  
  226. int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
  227. if(resultCode != ConnectionResult.SUCCESS){
  228.  
  229. if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)){
  230.  
  231. GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICE_RESOLUTION_REQUEST).show();
  232. }
  233. else
  234. {
  235. Toast.makeText(this, "This device is not support", Toast.LENGTH_SHORT).show();
  236. finish();
  237. }
  238. return false;
  239. }
  240. return true;
  241. }
  242.  
  243. @Override
  244. public void onMapReady(GoogleMap googleMap) {
  245. mMap = googleMap;
  246.  
  247. }
  248.  
  249. @Override
  250. public void onLocationChanged(Location location) {
  251.  
  252. mLastLocation = location;
  253. displayLocation();
  254.  
  255. }
  256.  
  257.  
  258. @Override
  259. public void onConnected(@Nullable Bundle bundle) {
  260.  
  261. displayLocation();
  262. startLocationUpdates();
  263.  
  264. }
  265.  
  266. private void startLocationUpdates() {
  267. if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  268. != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
  269. (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
  270.  
  271. {
  272. return;
  273. }
  274. mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
  275. }
  276.  
  277. @Override
  278. public void onConnectionSuspended(int i) {
  279.  
  280. mGoogleApiClient.connect();
  281.  
  282. }
  283.  
  284. @Override
  285. public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  286.  
  287.  
  288. }
  289.  
  290. @Override
  291. protected void onResume() {
  292. super.onResume();
  293. checkPlayServices();
  294. }
  295.  
  296. @Override
  297. protected void onStart() {
  298. super.onStart();
  299. if (mGoogleApiClient != null)
  300. mGoogleApiClient.connect();
  301. }
  302.  
  303. private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>>> {
  304.  
  305. ProgressDialog mDialog = new ProgressDialog(TrackingOrder.this);
  306.  
  307. @Override
  308. protected void onPreExecute() {
  309. super.onPreExecute();
  310. mDialog.setMessage("Please waiting...");
  311. mDialog.show();
  312. }
  313.  
  314. @Override
  315. protected List<List<HashMap<String, String>>> doInBackground(String... strings) {
  316.  
  317. JSONObject jsonObject;
  318. List<List<HashMap<String,String>>> routes = null;
  319. try{
  320. jsonObject = new JSONObject(strings[0]);
  321. DirectionJSONParser parser = new DirectionJSONParser();
  322.  
  323. routes = parser.parse(jsonObject);
  324.  
  325. }catch (JSONException e){
  326. e.printStackTrace();
  327. }
  328. return routes;
  329. }
  330.  
  331. @Override
  332. protected void onPostExecute(List<List<HashMap<String, String>>> lists) {
  333. mDialog.dismiss();
  334.  
  335. ArrayList points = null;
  336. PolylineOptions lineOptions = null;
  337.  
  338. for (int i = 0; i<lists.size(); i++){
  339.  
  340. points = new ArrayList();
  341. lineOptions = new PolylineOptions();
  342.  
  343. List<HashMap<String,String>> path = lists.get(i);
  344.  
  345. for (int j = 0; j<path.size(); j++){
  346.  
  347. HashMap<String,String> point = path.get(j);
  348.  
  349. double lat = Double.parseDouble(point.get("lat"));
  350. double lng = Double.parseDouble(point.get("lng"));
  351.  
  352. LatLng position = new LatLng(lat,lng);
  353.  
  354. points.add(position);
  355. }
  356.  
  357. lineOptions.addAll(points);
  358. lineOptions.width(12);
  359. lineOptions.color(Color.RED);
  360. lineOptions.geodesic(true);
  361. }
  362.  
  363. mMap.addPolyline(lineOptions);
  364. }
  365. }
Add Comment
Please, Sign In to add comment