如何在Android Studio Google Maps V2中显示驾车路线

时间:2014-05-06 23:07:00

标签: android google-maps android-studio google-maps-api-2 driving-directions

我有3个人的经纬度,我需要得到并显示我的位置和其他3个之间的路线,如下所示:

enter image description here

我的应用中有一个小数据库,有3个联系人,我的代码如下:

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();
}

}

并且只能显示这样的位置:

enter image description here

那么如何在我的位置和其他点之间以驾驶模式显示或显示路线? 我看到了任何示例或教程,但在某些示例中,示例存在库之间的差异,如:

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;

用于ADT或Eclipse和

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

或GeoPoint相当于LatLng?

或者我错了什么或遗忘了什么,我如何将数据库中的LatLng对象放在数组中以显示路径?任何帮助?谢谢伙计们

2 个答案:

答案 0 :(得分:2)

如果要显示两点之间的路线,则需要使用Google Directions API检索两个位置之间的子点。

例如,您可以使用此网址https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal

获取多伦多和蒙特利尔之间的路线作为JSON

获得这些积分后,您只需使用map.addPolyline方法将它们放到地图上。

答案 1 :(得分:-2)

你应该试试这个 Driving route directions