Saturday, July 21, 2018

Route draw between two locations google map in android

 implementation 'com.google.maps:google-maps-services:0.1.20'



DateTime now = new DateTime();
try {
             final DirectionsResult result = DirectionsApi.newRequest(getGeoContext())
                           .mode(TravelMode.DRIVING).origin(origin)
                            .destination(destination).departureTime(now)
                            .await();


                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            addPolyline(result);
                        }
                    });

                    Log.e("ZX", "ZX");

                } catch (ApiException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


  private void addPolyline(DirectionsResult results) {
        List<LatLng> decodedPath = results.routes[0].overviewPolyline.decodePath();
        ArrayList<com.google.android.gms.maps.model.LatLng> pathlist = new ArrayList<>();
        for (int i = 0; i < decodedPath.size(); i++) {
            pathlist.add(new com.google.android.gms.maps.model.LatLng(decodedPath.get(i).lat, decodedPath.get(i).lng));
        }
        googleMap.addPolyline(new PolylineOptions().addAll(pathlist).color(R.color.linecolor));
    }

    private GeoApiContext getGeoContext() {
        GeoApiContext geoApiContext = new GeoApiContext();
        return geoApiContext.setQueryRateLimit(3)
                .setApiKey(getString(R.string.google_maps_key))
                .setConnectTimeout(1, TimeUnit.SECONDS)
                .setReadTimeout(1, TimeUnit.SECONDS)
                .setWriteTimeout(1, TimeUnit.SECONDS);
    }

Date Picker Dialog

int mYear, mMonth, mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Calendar cal = new GregorianCalendar(year, monthOfYear, dayOfMonth); Date current = cal.getTime(); int diff1 = new Date().compareTo(current); if (diff1 < 0) {
                    Snackbar.make(coordinatorLayout, "valid Message", Snackbar.LENGTH_LONG);
} else { SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy"); datePickerTv.setText(dateFormat.format(cal.getTime())); } } }, mYear, mMonth, mDay); datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() - 1000); datePickerDialog.show();