"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Get Driving Directions Using Google Maps API v2?

How to Get Driving Directions Using Google Maps API v2?

Published on 2024-11-09
Browse:862

How to Get Driving Directions Using Google Maps API v2?

Getting Driving Directions with Google Maps API v2

The request you've mentioned in your question utilizes the addPolyline method of the Google Maps API to draw a straight line between two points. This method is intended for displaying simple lines on the map, not for retrieving driving directions.

To obtain detailed driving instructions between two locations, you should integrate the Google Maps Directions API into your application. The library created by AKExorcist, as mentioned in the answer you provided, is a convenient option for this task.

Here's a sample code snippet demonstrating how to use this library to retrieve driving directions:

import akexorcist.googledirection.DirectionCallback;
import akexorcist.googledirection.GoogleDirection;
import akexorcist.googledirection.constant.TransportMode;
import akexorcist.googledirection.model.Direction;

// Initialize GoogleDirection
GoogleDirection googleDirection = new GoogleDirection(apiKey);

// Set the departure and arrival locations
LatLng origin = new LatLng(12.917745600000000000, 77.623788300000000000);
LatLng destination = new LatLng(12.842056800000000000, 7.663096499999940000);

// Request directions
googleDirection.withTransportMode(TransportMode.DRIVING)
        .withOrigin(origin)
        .withDestination(destination)
        .execute(new DirectionCallback() {
            @Override
            public void onDirectionSuccess(Direction direction, String rawBody) {
                // Process and display driving directions
            }

            @Override
            public void onDirectionFailure(Throwable t) {
                // Handle direction retrieval error
            }
        });

This code uses the GoogleDirection library to retrieve driving directions between the specified points. Upon successful retrieval, the onDirectionSuccess callback method will be called, providing access to the direction object that contains the instructions, duration, and distance.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3