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.
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.
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: