Chivorn
Chivorn

Reputation: 2363

How to draw driving route on Google Map Android and apply walking mode when no driving route available like Google Map app?

I am trying to build an application with google direction API on Android. I want apply direction mode as driving but when there is no route available, I want to apply dot polylines like Google Map App.

Bellow is sample code for apply polylines pattern for route from atabouraya's answer but it is opposite of what I want to apply.

public static final int PATTERN_GAP_LENGTH_PX = 20;
public static final PatternItem DOT = new Dot();
public static final PatternItem DASH = new Dash(PATTERN_DASH_LENGTH_PX);
public static final PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);
public static final List<PatternItem> PATTERN_POLYGON_ALPHA = Arrays.asList(GAP, DASH);

 private void drawDashedLeg(GoogleMap googleMap, Route route) {
    PolylineOptions polyOptions = new PolylineOptions();
    polyOptions.color(ContextCompat.getColor(getContext(), R.color.coolgrey));
    polyOptions.addAll(route.getPoints());
    polyOptions.pattern(PATTERN_POLYGON_ALPHA);
    Polyline polyline = googleMap.addPolyline(polyOptions);
    polylines.add(polyline);
}

And here is another reference for how to draw curved dashed line on Google Map as they focus only how to draw curved line from one location to another.

My purpose is how to draw route from one location to another as driving route and apply walking mode when there is no route available.

Anyone who have any idea on this please help to share it. Thank you.

Upvotes: 0

Views: 627

Answers (1)

Chivorn
Chivorn

Reputation: 2363

For my temporary solution, I just separate polyline to 3 parts:

  1. From origin (driver's current location) to first route (available driving route)
  2. From first available driving route until the last of driving route
  3. From last available driving route to destination (store's location)

Here is my sample code after decode polylinePoint

if (routeList.size > 0) {
    val polylineOptionsWalkingStart = getPolylineOptionsConfig(routeI)
    val polylineOptionsWalingEnd = getPolylineOptionsConfig(routeI)
    val polylineOptionsDriving = getPolylineOptionsConfig(routeI)

    // Add walking route from origin to first driving route
    polylineOptionsWalkingStart.add(origin, routeList[0]!!)
        .pattern(MapUtil.PATTERN_POLYGON_ALPHA)

    // Add walking route from last driving route to destination
    polylineOptionsWalingEnd.add(
        routeList[routeList.size - 1]!!,
        destination
    ).pattern(MapUtil.PATTERN_POLYGON_ALPHA)

    // Add driving route from origin to destination
    polylineOptionsDriving.addAll(routeList)

    // Adding route for driving and walking mode on the map
    polylineWalkingStart = mMap.addPolyline(polylineOptionsWalkingStart)
    polylineWalkingEnd = mMap.addPolyline(polylineOptionsWalingEnd)
    polylineDriving = mMap.addPolyline(polylineOptionsDriving)
}

and here is my PolylineOptions configuration

private fun getPolylineOptionsConfig(mainRoute: Int): PolylineOptions {
    val polylineOptions = PolylineOptions().width(18f).geodesic(true)
    return when (mainRoute) {
        0 -> {
            polylineOptions.color(Color.parseColor("#B1C001"))
        }
        1 -> {
            polylineOptions.color(Color.parseColor("#0099D8"))
        }
        2 -> {
            polylineOptions.color(Color.parseColor("#1777EB"))
        }
        else -> {
            polylineOptions.color(Color.parseColor("#515C6F"))
        }
    }
}

And here is polyline pattern

private val DOT: PatternItem = Dot()
val PATTERN_POLYGON_ALPHA = listOf(DOT)

If you have another better solution please share your answer here. Anyway, I hope my answer can help you in this situation.

Upvotes: 1

Related Questions