2

I am using Google Directions API to get routes between two locations with Way Points.

Currently what I am doing is getting direction details between two locations using Direction API and showing the result in Google Maps integrated inside my application. It works well as expected. This is how I did it:

    private DirectionsResult getDirectionsDetails(String origin,String destination,TravelMode mode) {
    Log.i("testtt"," Origin "+origin+" Destination "+destination);
    DateTime now = new DateTime();
    try {
        return DirectionsApi.newRequest(getGeoContext())
                .mode(mode)
                .origin(origin)
                .waypoints(waypoints.toArray(new String[0]))
                .destination(destination)
                .departureTime(now)
                .await();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (com.google.maps.errors.ApiException e) {
        e.printStackTrace();
        return null;
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {

    setupGoogleMapScreenSettings(googleMap);
    DirectionsResult results = getDirectionsDetails(origin,destination,TravelMode.DRIVING);
    if (results != null) {
        addPolyline(results, googleMap);
        positionCamera(results.routes[overview], googleMap);
        addMarkersToMap(results, googleMap);
    }
}

private void setupGoogleMapScreenSettings(GoogleMap mMap) {
    mMap.setBuildingsEnabled(true);
    mMap.setIndoorEnabled(true);
    mMap.setTrafficEnabled(true);
    UiSettings mUiSettings = mMap.getUiSettings();
    mUiSettings.setZoomControlsEnabled(true);
    mUiSettings.setCompassEnabled(true);
    mUiSettings.setMyLocationButtonEnabled(true);
    mUiSettings.setScrollGesturesEnabled(true);
    mUiSettings.setZoomGesturesEnabled(true);
    mUiSettings.setTiltGesturesEnabled(true);
    mUiSettings.setRotateGesturesEnabled(true);
}

private void addMarkersToMap(DirectionsResult results, GoogleMap mMap) {
    mMap.addMarker(new MarkerOptions().position(new LatLng(results.routes[overview].legs[overview].startLocation.lat,results.routes[overview].legs[overview].startLocation.lng)).title(results.routes[overview].legs[overview].startAddress));
    mMap.addMarker(new MarkerOptions().position(new LatLng(results.routes[overview].legs[overview].endLocation.lat,results.routes[overview].legs[overview].endLocation.lng)).title(results.routes[overview].legs[overview].endAddress).snippet(getEndLocationTitle(results)));
}

private void positionCamera(DirectionsRoute route, GoogleMap mMap) {
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(route.legs[overview].startLocation.lat, route.legs[overview].startLocation.lng), 12));
}

private void addPolyline(DirectionsResult results, GoogleMap mMap) {
    List<LatLng> decodedPath = PolyUtil.decode(results.routes[overview].overviewPolyline.getEncodedPath());
    mMap.addPolyline(new PolylineOptions().addAll(decodedPath));
}

But what I want is I want to load this direction result in the external Google Maps app. What I am asking is is there any way to pass the DirectionsResult object to Google Maps application via Intent so that it will show the routes in the app. Reason why I want this is to avoid integrating Google Maps API to the project as it is not completely free anymore. Pricing details

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
Sudheesh Mohan
  • 2,560
  • 3
  • 21
  • 38
  • Is it important that the route has to be same as you calculated in your application or a different route will also work? – nimi0112 Jul 17 '18 at 12:30
  • @nimi0112 The requirement is the route should be shortest. So the direction API already calculates shortest route, so the different route will be longer? – Sudheesh Mohan Jul 17 '18 at 12:44

1 Answers1

1

As I can see in your code, that you are not performing any calculations on the DirectionsDetails given by DirectionsApi you can open the pass your location coordinates in google maps.

By default, Google maps always loads the best available route according to current traffic and time:

String geoUri = "http://maps.google.com/maps?q=loc:" + lat + "," + lng + " (" + mTitle + ")";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
context.startActivity(intent);

If you want to add waypoints in between your source and destination you can have a look at this answer.

If you want, you can find the route through DirectionsApi and process the data for your internal analytics for eg. approx time to travel between his location and new location, distance etc.

nimi0112
  • 2,065
  • 1
  • 18
  • 32
  • Will this display the direction details with markers in google map application? – Sudheesh Mohan Jul 17 '18 at 13:13
  • @SudheeshMohan Yes, you can try it out by placing this code in a sample app's on create method – nimi0112 Jul 17 '18 at 13:15
  • This is mapurls right? https://developers.google.com/maps/documentation/urls/guide – Sudheesh Mohan Jul 17 '18 at 13:31
  • @SudheeshMohan The above code will work for both the cases `If Google Maps app for Android is installed and active, the URL launches Google Maps in the Maps app and performs the requested action. If the Google Maps app is not installed or is disabled, the URL launches Google Maps in a browser and performs the requested action.` – nimi0112 Jul 17 '18 at 13:35
  • Ok. If I am launching external Google map application using map url, will there be any limits for number of way points that can be added in the url? – Sudheesh Mohan Jul 17 '18 at 17:03