How to display the driving routes in Android Studio Google Maps V2

user_Dennis_Mostajo picture user_Dennis_Mostajo · May 7, 2014 · Viewed 21.5k times · Source

well I have the latitude and longitude of 3 people, I need to get and display the route between my location and the other 3 like this:

enter image description here

I have a little data Base in my app with 3 contacts and my code is like:

package androiddatabase.app;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;

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



/**
 * Created by Dennis and Rodrigo on 5/6/2014.
 */
public class RutasActivity extends FragmentActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_ruta);

    GoogleMap mapa = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    mapa.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    mapa.setMyLocationEnabled(true);
    mapa.getMyLocation();

    CargarContactos(this, mapa);

        //--------------------------------//
        //           GREETINGS            //
        //          FROM BOLIVIA!!!       //
        //              n_n               //
        //--------------------------------//
}

private  void  CargarContactos(Context context,GoogleMap mapa){
    myApp.DBManager manager = new myApp.DBManager(context);
    Cursor cursor = manager.CargarMapa();
    if (cursor.moveToFirst()) {
        do
        {
            if (cursor.getString(4).toString().contains("1")) {
                mapa.addMarker(new MarkerOptions()
                        .position(
                                new LatLng(Double.parseDouble(cursor.getString(2)),
                                        Double.parseDouble(cursor.getString(3)))
                        )
                        .title(cursor.getString(7) + " - " + cursor.getString(1))
                        .snippet("Fecha: " + cursor.getString(5) + " Monto: " + cursor.getString(6))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.familia)));
            }
            else
            {
                mapa.addMarker(new MarkerOptions()
                        .position(
                                new LatLng(Double.parseDouble(cursor.getString(2)),
                                        Double.parseDouble(cursor.getString(3)))
                        )
                        .title(cursor.getString(7) + " - " + cursor.getString(1))
                        .snippet("Fecha: " + cursor.getString(5) + " Monto: " + cursor.getString(6))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.amigos)));
            }
        }
        while(cursor.moveToNext());
    }

    cursor.close();
    manager.CloseManager();
}

}

and only can show the positions like this:

enter image description here

so how can I show or display the route in driving mode between my location and other points? I see any examples or tutorials but in someones examples exists differences between the libraries like:

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

for ADT or Eclipse and

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

for Android Studio

or GeoPoint is the equivalent to LatLng?

or am I wrong in something or forget something, how can I put my LatLng objects from my database in an array for show the route? any help? thanks Guys

Answer

Emre Koç picture Emre Koç · Jan 6, 2015

If you want to display route between two points, you need to use Google Directions API to retrieve sub points between 2 locations.

For example, you can get route between Toronto and Montreal as JSON with this url https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal

After getting these points you just have to put them onto map with map.addPolyline method.