EasyRoutes allows you to easily draw routes through the google maps address api.

Related tags

Maps EasyRoutes
Overview

EasyRoutes

EasyRoutes allows you to easily draw routes through the google maps address api.

Note: You need to generate an API key from the google console with the directions API and add INTERNET permission in manifest.

Setup

Gradle: Add in your root build.gradle.

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

Add the dependency in your app build.gradle.

dependencies {
	        implementation 'com.github.AntonioHReyes:EasyRoutes:1.0.9'
	}

if you have the next error:

Caused by: org.gradle.api.InvalidUserCodeException: Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'build.gradle'

modify your settings.gradle to:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
        maven { url 'https://jitpack.io' }
    }
}

Otherwise you have to use library directly in your project.

USAGE

first you need to define an instance of EasyRoutesDirections with which you can pass properties to query the google directions API.

a full properties example:

val placeDirections = EasyRoutesDirections(
            originPlace = "Ixhuatlán del café, Veracruz, México",
            destinationPlace = "Hermosillo, Sonora, México",
            apiKey = getString(R.string.google_maps_key),
            waypointsLatLng = arrayListOf(
                LatLng(20.077550759401632, -98.36895687240255),
                LatLng(19.43488491844211, -99.13136781301444)
            ),
            waypointsPlaces = arrayListOf(
                "Santiago de Queretaro",
                "Aguascalientes"
            ),
            showDefaultMarkers= false,
            transportationMode = TransportationMode.WALKING
        )

you can mix LatLng and String places of origin and destination.

val placeAndLatLng = EasyRoutesDirections(
            originPlace = "Oaxaca",
            destinationLatLng = LatLng(19.417708496429597, -102.05174097597963),
            apiKey = getString(R.string.google_maps_key)
        )

val latLngAndPlace = EasyRoutesDirections(
    originLatLng = LatLng(19.417708496429597, -102.05174097597963),
    destinationPlace = "Merida, Yucatan, México",
    apiKey = getString(R.string.google_maps_key)
)

Note: you can't define a two origins or two destinations.

a description of properties:

Parameter Type Description
api_key String Required. Your API key from google cloud
originLatLng LatLng Your origin in latitude and longitude values
originPlace String Your origin in String format for example Oaxaca
destinationLatLng LatLng Your destination in latitude and longitude values
destinationPlace String Your destination in String format for example Aguascalientes
waypointsLatLng Array<LatLng> Specifies an array of intermediate locations in latitude and longitude values
waypointsPlaces Array<String> Specifies an array of intermediate locations in String format
transportationMode TransportationMode Specify the transportation mode to use by default is DRIVING
showDefaultMarkers Boolean Flag to show default markers in origin and destination

next you need to define a EasyDrawer Builder to customize the route.

val routeDrawer = EasyRoutesDrawer.Builder(mMap)
            .pathWidth(10f)
            .pathColor(Color.GREEN)
            .geodesic(true)
            .previewMode(false)
            .build()

a description of properties:

Parameter Type Description
pathWidth Float Width of you path drawn by default is 12f
pathColor Int Color of you path by default is Color.MAGENTA
geodesic Boolean Specifies whether to draw each segment of this polyline as a geodesic by default is true
previewMode Boolean Specify the property to paint the path if true selects overview Polyline defaults to true

Note: if you like view the route with precision use previewMode in false.

you can pass a custom polyline for better handling.

val customPolylineOptions = PolylineOptions()
customPolylineOptions.color(ContextCompat.getColor(this@MapsActivity, R.color.red))
customPolylineOptions.width(15f)

val routeDrawerWithCustomPolyline = EasyRoutesDrawer.Builder(mMap, customPolylineOptions)
            .previewMode(false)
            .build()

finally use a map extension function fo pain the route.

mMap.drawRoute(
    context = this@MapsActivity,
    easyRoutesDirections = placeDirections,
    routeDrawer = routeDrawer,
    markersListCallback = {markers -> markersList.addAll(markers) },
    googleMapsLink = { url -> Log.d("GoogleLink", url)}
){ legs ->
    legs?.forEach {
        Log.d("Point startAddress:", it?.startAddress.toString())
        Log.d("Point endAddress:", it?.endAddress.toString())
        Log.d("Distance:", it?.distance.toString())
        Log.d("Duration:", it?.duration.toString())
    }
}

you can use markersListCallback to get the origin a destination markers painted by default. you can use googleMapsLink to get the url of google maps with the route. you can use legsCallback to get the distance and duration of points in the route.

additional you can use getGoogleMapsLink to get a url of google maps with the route:

binding.mapsDirections.setOnClickListener { 
    val url = getGoogleMapsLink(placeDirections) //EasyRouteDirections instance like parameter

    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
    startActivity(intent) //Open google maps native app
}

you can remove the route and markers with:

routeDrawer.removeRoute()
routeDrawerWithCustomPolyline.removeRoute()

markersList.forEach { 
    it.remove()
}

Screnshot

image image

Buy me a coffee

paypal

Developed By

Antonio Huerta Reyes - [email protected]

Antonio Huerta

You might also like...
An app to search nearby businesses on Google Maps & Add Grocery Items to List!
An app to search nearby businesses on Google Maps & Add Grocery Items to List!

GoStore: Internship Program Project A mobile app is built where the user can search for his nearby locations based on his requirement. Whenever the us

Maps SDK for Android Utility Library
Maps SDK for Android Utility Library

Maps SDK for Android Utility Library Description This open-source library contains utilities that are useful for a wide range of applications using th

Interactive, thoroughly customizable maps in native Android powered by vector tiles and OpenGL.
Interactive, thoroughly customizable maps in native Android powered by vector tiles and OpenGL.

Interactive, thoroughly customizable maps in native Android powered by vector tiles and OpenGL.

TileOverlay component for react-native-maps

TileOverlay component for react-native-maps

 App usage tracker which maps your app usage to geo location.
App usage tracker which maps your app usage to geo location.

Guilt Guilt is an inspiration from Meta (pun intended), it tracks the apps usage and maps it with geo location data where the app was last used. The a

This project allows you to  calculate the route between two locations and displays it on a map.
This project allows you to calculate the route between two locations and displays it on a map.

Google-Directions-Android This project allows you to calculate the direction between two locations and display the route on a Google Map using the Goo

Android library that allows you to determine your location in a few of lines
Android library that allows you to determine your location in a few of lines

locsimple Android library that allows you to determine your location in some lines! Benefits: automatic processing of permissions processing of enabli

BlueMap addtion plugin. Add a marker command to the game to easily setup your markers ingame without touching configs
BlueMap addtion plugin. Add a marker command to the game to easily setup your markers ingame without touching configs

BlueMap Marker Manager - Download BlueMap here BlueMap supports a variety of marker setups by default, but it requires some work from editing configs

This is a repo for implementing Map pan or drag (up, down, right ,left) to load more places on the Google Map for Android

Challenge Display restaurants around the user’s current location on a map ○ Use the FourSquare Search API to query for restaurants: https://developer.

Releases(1.0.11)
Owner
Antonio Huerta Reyes
Senior developer Android | Front-End developer
Antonio Huerta Reyes
malik dawar 87 Sep 18, 2022
My Maps displays a list of maps, each of which show user-defined markers with a title, description, and location. The user can also create a new map. The user can save maps and load them in from previous usages of the app.

My Maps Bryant Jimenez My Maps displays a list of maps, each of which show user-defined markers with a title, description, and location. The user can

null 0 Nov 1, 2021
Android Maps Extensions is a library extending capabilities of Google Maps Android API v2.

Android Maps Extensions Library extending capabilities of Google Maps Android API v2. While Google Maps Android API v2 is a huge leap forward comapare

Maciej Górski 408 Dec 15, 2022
Scale bar for Android Maps (Google Maps, OSM, MapBox, Yandex)

Map Scale View Scale view for any Android Maps SDK (not only Google Maps) Contributing I encourage you to participate in this project. Feel free to op

Stas Parshin 109 Nov 18, 2022
🍃 Organic Maps is an Android & iOS offline maps app for travelers, tourists, hikers, and cyclists based on top of crowd-sourced OpenStreetMap data and curated with love by MapsWithMe founders.

?? Organic Maps is an Android & iOS offline maps app for travelers, tourists, hikers, and cyclists based on top of crowd-sourced OpenStreetMap data and curated with love by MapsWithMe founders. No ads, no tracking, no data collection, no crapware.

Organic Maps 4.3k Dec 31, 2022
Maps application in Android Studio using the Maps SDK for android

Google-Maps-Application Maps application in Android Studio using the Maps SDK for android. This app was initially developed in Java, but then was conv

Kyle McInnis 0 Nov 30, 2021
An android app that uses Google Maps API and SDK to track a user's location and calculate the total distance travelled

Bike Rush is an android app that uses Google Maps API and SDK to track a user's location and calculate the total distance travelled by him or her along with time and average speed.

Ishant Chauhan 21 Nov 14, 2022
Curve-Fit is an Android library for drawing curves on Google Maps

Curve-Fit Android library for drawing curves on Google Maps. This library uses Bezier cubic equation in order to compute all intermediate points of a

Sarweshkumar C R 63 Mar 7, 2021
Demo de uso de google maps en Android, charla para el GDG Chimbote

mapasbasico Demo de uso de google maps en Android, charla para el GDG Chimbote Puedes usar este proyecto como base para trabajar con mapas en Android.

Whiston Kendrick Borja Reyna 4 Sep 17, 2021
Membuat Custom Tooltip Marker Google Maps

Custom-Tooltip-Marker Membuat Custom Tooltip Marker Google Maps Tutorial Build with Android Studio https://youtu.be/E8ND0YThNiU Tutorial Build with St

Azhar Rivaldi 5 Feb 17, 2022