Home » Android » Android: How to draw route directions google maps API V2 from current location to destination

Android: How to draw route directions google maps API V2 from current location to destination

Posted by: admin April 23, 2020 Leave a comment

Questions:

How to draw route direction from current location to destination which (latitude and longitude), i have code as below:

import android.os.Bundle;
import com.actionbarsherlock.app.SherlockActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class DetailMapActivity extends SherlockActivity {
    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_detail_map);

        LatLng TO_DESTINATION = new LatLng(-6.33438, 106.74316);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
        map.setMyLocationEnabled(true);

        map.addMarker(new MarkerOptions().position(TO_DESTINATION).title("Destination Title")
                .snippet("Destination Description"));

        map.moveCamera(CameraUpdateFactory.newLatLngZoom(TO_LOCATION, 40));

        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    }
}

I want to draw from current location by provider network or gps, and then draw the route by driving to destination.

I’m using android google apis V2.

Thanks for your help.

How to&Answers:

This works great for me:
It’s not my code, I took it from a great answer at stackoverflow but I can’t find this answer now, so here is the code:

Add this class to your project:

Then use this class for your needs.
For example to draw directions:

The sourcePosition, destPosition are from the LatLng type, and you give them the wanted points.

I wrote here parts from my code that I think could help, Any question is welcome.

Answer:

Using the dvrm solution GMapV2Direction class, it’s a good solution and it’s still working, but now you need to implement the getDocument method in a new thread to allow network connections. This is an example for an AsyncTask for this method, the rest of the class is the same. You can handle it with a Handle or implement your own interface callback to get the Docmuent xml response.

}

This is the async handler method for the activity:

One more thing, if the duration value is wrong, solution for it is to change one line of the code of GMapV2Direction class:

instead:

Answer:

I know it’s too late but worth. I have searched and tried a lot of example but didn’t get desired result so I found this amazing library which give you the desired result. You have to write bunch of line.

Link: https://github.com/jd-alexander/Google-Directions-Android

Import library:

implementation ‘com.github.jd-alexander:library:1.1.0’

I am implementing the code inside onMarkerClick so If a user click on marker then it will route from his current location to marker location but you can change this as per your requirement.

Implement RoutingListener

Override Methods

Here is the example project you can check this code as well.

https://github.com/jd-alexander/Google-Directions-Android/blob/master/sample/src/main/java/com/directions/sample/MainActivity.java

Result

enter image description here

Answer:

Here i am posting complete code for drawing route between current location and another place on google map.

For more details : Visit draw route on google map with current location

PointParser class

FetchURL class

Main Activity

If you have any error then check out original tutorial given at the starting of this answer.

Answer: