Skip to content

Direction Api In Android Google Map. This is an android library which provides you a direction path between two points (locations) such as device location to destination location. To use this library, you need to create a project on Goolge Cloud Console and enabble the Map SDK along with Google Direction Api.

License

Notifications You must be signed in to change notification settings

dangiashish/Google-Direction-Api

Repository files navigation

Google Direction Api 🗺️

CodeFactor

ℹ️ Library Details :

This is an android library which provides you a direction path between two points (locations) such as device location to destination location. To use this library, you need to create a project on Goolge Cloud Console and enabble the Map SDK along with Google Direction Api. After the all completion of the setup, just follow these simple steps to implement the code in your project.

Demo

Click to watch video demo
OR
Youtube Tutotial Of Implemetation
Image Video demo

ℹ️ Implementation Process :

Gradle

Add jitpack repository in your build.gradle (project-level) file :

allprojects {
      repositories {
	...
	maven { url 'https://jitpack.io' }
	}
}
OR

in your settings.gradle

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Add dependency :

Add dependency in your build.gradle (module-level) file :

dependencies {
	  implementation 'com.github.dangiashish:Google-Direction-Api:1.6'
}

in your map activity

public void getRoutePoints(LatLng start, LatLng end) {
       if (start == null || end == null) {
           Toast.makeText(this, "Unable to get location", Toast.LENGTH_LONG).show();
           Log.e("TAG", " latlngs are null");
       } else {
           RouteDrawing routeDrawing = new RouteDrawing.Builder()
                .context(MainActivity.this)  // pass your activity or fragment's context
                .travelMode(AbstractRouting.TravelMode.DRIVING)
                .withListener(this).alternativeRoutes(true)
                .waypoints(userLoc, destLoc)
                .build();
        routeDrawing.execute();
       }

}
⚠ Note : Please restrict your api keys on GCP for your specific android package name

Implement the RouteListener interface class in your Activity/Fragment to override the operations

On Route Failed

@Override
public void onRouteFailure(ErrorHandling e) {
    Log.w("TAG", "onRoutingFailure: " + e);
}

On Route Start

@Override
public void onRouteStart() {
    Log.d("TAG", "yes started");
}
    

On Route Success

@Override
public void onRouteSuccess(ArrayList<RouteInfoModel> routeInfoModelArrayList, int routeIndexing) {
     if (polylines != null) {
         polylines.clear();
     }
     PolylineOptions polylineOptions = new PolylineOptions();
     ArrayList<Polyline> polylines = new ArrayList<>();
     for (int i = 0; i < routeInfoModelArrayList.size(); i++) {
         if (i == routeIndexing) {
             Log.e("TAG", "onRoutingSuccess: routeIndexing" + routeIndexing);
             polylineOptions.color(Color.BLACK);
             polylineOptions.width(12);
             polylineOptions.addAll(routeInfoModelArrayList.get(routeIndexing).getPoints());
             polylineOptions.startCap(new RoundCap());
             polylineOptions.endCap(new RoundCap());
             Polyline polyline = map.addPolyline(polylineOptions);
             polylines.add(polyline);
	  }
     }

}

On Route Canceled

@Override
public void onRouteCancelled() {
    Log.d("TAG", "route canceled")
    // restart your route drawing
}

Connect with me:

Github Profile LinkedIn Instagram

License :

MIT License

Copyright (c) 2023 Ashish Dangi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

Direction Api In Android Google Map. This is an android library which provides you a direction path between two points (locations) such as device location to destination location. To use this library, you need to create a project on Goolge Cloud Console and enabble the Map SDK along with Google Direction Api.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages