Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TrackingOrder extends FragmentActivity implements OnMapReadyCallback,
- GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener,
- LocationListener{
- private GoogleMap mMap;
- private final static int PLAY_SERVICE_RESOLUTION_REQUEST = 1000;
- private final static int LOCATION_PERMISSION_REQUEST = 1001;
- private Location mLastLocation;
- private GoogleApiClient mGoogleApiClient;
- private LocationRequest mLocationRequest;
- private LocationCallback mLocationCallback;
- private FusedLocationProviderClient mFusedLocationProviderClient;
- private static int UPDATE_INTERVAL = 1000;
- private static int FATEST_INTERVAL = 5000;
- private static int DISPLACEMENT = 10;
- private IGeoCoordinates mService;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_tracking_order);
- mService = Common.getGeoCodeService();
- if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
- (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
- {
- requestRuntimePermission();
- }
- else{
- if(checkPlayServices()){
- buildGoogleApiClient();
- createLocationRequest();
- createLocationCallBack();
- }
- }
- mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
- displayLocation();
- // Obtain the SupportMapFragment and get notified when the map is ready to be used.
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- }
- private void requestRuntimePermission() {
- ActivityCompat.requestPermissions(this, new String[]
- {
- Manifest.permission.ACCESS_COARSE_LOCATION,
- Manifest.permission.ACCESS_FINE_LOCATION},
- LOCATION_PERMISSION_REQUEST);
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- switch (requestCode) {
- case LOCATION_PERMISSION_REQUEST:
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- if (checkPlayServices()) {
- buildGoogleApiClient();
- createLocationRequest();
- displayLocation();
- }
- }
- break;
- }
- }
- private void displayLocation() {
- if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
- (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
- {
- requestRuntimePermission();
- }
- else
- {
- mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
- if (mLastLocation != null)
- {
- double latitude = mLastLocation.getLatitude();
- double longitude = mLastLocation.getLongitude();
- //Add Marker in location and move the camera
- LatLng yourLocation = new LatLng(latitude, longitude);
- mMap.addMarker(new MarkerOptions().position(yourLocation).title("Your Location"));
- mMap.moveCamera(CameraUpdateFactory.newLatLng(yourLocation));
- mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
- //After add Marker for location, Add Marker for Order and draw route
- drawRoute(yourLocation, Common.currentRequest.getAddress());
- }
- else
- {
- //Toast.makeText(this, "Couldn't get the location", Toast.LENGTH_SHORT).show();
- Log.d("DEBUG", "Couldn't get the location" );
- }
- }
- }
- private void drawRoute(final LatLng yourLocation, String address) {
- mService.getGeoCode(address).enqueue(new Callback<String>() {
- @Override
- public void onResponse(Call<String> call, Response<String> response) {
- try{
- JSONObject jsonObject = new JSONObject(response.body().toString());
- String lat = ((JSONArray)jsonObject.get("results"))
- .getJSONObject(0)
- .getJSONObject("geometry")
- .getJSONObject("location")
- .get("lat").toString();
- String lng = ((JSONArray)jsonObject.get("results"))
- .getJSONObject(0)
- .getJSONObject("geometry")
- .getJSONObject("location")
- .get("lng").toString();
- LatLng orderLocation = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
- Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.deliverybox);
- bitmap = Common.scaleBitmap(bitmap, 70, 70);
- MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
- .title("Order of " + Common.currentRequest.getPhone())
- .position(orderLocation);
- mMap.addMarker(marker);
- //draw route
- mService.getDirections(yourLocation.latitude + "," + yourLocation.longitude,
- orderLocation.latitude + "," + orderLocation.longitude)
- .enqueue(new Callback<String>() {
- @Override
- public void onResponse(Call<String> call, Response<String> response) {
- new ParserTask().execute(response.body().toString());
- }
- @Override
- public void onFailure(Call<String> call, Throwable t) {
- }
- });
- }
- catch (JSONException e){
- e.printStackTrace();
- }
- }
- @Override
- public void onFailure(Call<String> call, Throwable t) {
- }
- });
- }
- private void createLocationRequest() {
- mLocationRequest = new LocationRequest();
- mLocationRequest.setInterval(UPDATE_INTERVAL);
- mLocationRequest.setFastestInterval(FATEST_INTERVAL);
- mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
- mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
- }
- private void createLocationCallBack() {
- mLocationCallback = new LocationCallback(){
- @Override
- public void onLocationResult(LocationResult locationResult) {
- super.onLocationResult(locationResult);
- mLastLocation = locationResult.getLastLocation();
- mLocationRequest = LocationRequest.create();
- mGoogleApiClient.connect();
- onLocationChanged(mLastLocation);
- }
- };
- }
- protected synchronized void buildGoogleApiClient() {
- mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- mGoogleApiClient.connect();
- }
- private boolean checkPlayServices() {
- int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
- if(resultCode != ConnectionResult.SUCCESS){
- if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)){
- GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICE_RESOLUTION_REQUEST).show();
- }
- else
- {
- Toast.makeText(this, "This device is not support", Toast.LENGTH_SHORT).show();
- finish();
- }
- return false;
- }
- return true;
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- }
- @Override
- public void onLocationChanged(Location location) {
- mLastLocation = location;
- displayLocation();
- }
- @Override
- public void onConnected(@Nullable Bundle bundle) {
- displayLocation();
- startLocationUpdates();
- }
- private void startLocationUpdates() {
- if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
- (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
- {
- return;
- }
- mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
- }
- @Override
- public void onConnectionSuspended(int i) {
- mGoogleApiClient.connect();
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- }
- @Override
- protected void onResume() {
- super.onResume();
- checkPlayServices();
- }
- @Override
- protected void onStart() {
- super.onStart();
- if (mGoogleApiClient != null)
- mGoogleApiClient.connect();
- }
- private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>>> {
- ProgressDialog mDialog = new ProgressDialog(TrackingOrder.this);
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- mDialog.setMessage("Please waiting...");
- mDialog.show();
- }
- @Override
- protected List<List<HashMap<String, String>>> doInBackground(String... strings) {
- JSONObject jsonObject;
- List<List<HashMap<String,String>>> routes = null;
- try{
- jsonObject = new JSONObject(strings[0]);
- DirectionJSONParser parser = new DirectionJSONParser();
- routes = parser.parse(jsonObject);
- }catch (JSONException e){
- e.printStackTrace();
- }
- return routes;
- }
- @Override
- protected void onPostExecute(List<List<HashMap<String, String>>> lists) {
- mDialog.dismiss();
- ArrayList points = null;
- PolylineOptions lineOptions = null;
- for (int i = 0; i<lists.size(); i++){
- points = new ArrayList();
- lineOptions = new PolylineOptions();
- List<HashMap<String,String>> path = lists.get(i);
- for (int j = 0; j<path.size(); j++){
- HashMap<String,String> point = path.get(j);
- double lat = Double.parseDouble(point.get("lat"));
- double lng = Double.parseDouble(point.get("lng"));
- LatLng position = new LatLng(lat,lng);
- points.add(position);
- }
- lineOptions.addAll(points);
- lineOptions.width(12);
- lineOptions.color(Color.RED);
- lineOptions.geodesic(true);
- }
- mMap.addPolyline(lineOptions);
- }
- }
Add Comment
Please, Sign In to add comment