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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23506012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:12:41  来源:igfitidea点击:

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

androidgoogle-mapsandroid-studiogoogle-maps-api-2driving-directions

提问by user_Dennis_Mostajo

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:

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

enter image description here

在此处输入图片说明

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

我的应用程序中有一个包含 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();
}

}

}

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

对于 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

安卓工作室

or GeoPoint is the equivalent to LatLng?

或 GeoPoint 相当于 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

或者我在某些事情上错了或忘记了某些事情,我如何将我的数据库中的 LatLng 对象放入一个数组中以显示路线?有什么帮助吗?谢谢你们

回答by Emre Ko?

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

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

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

例如,您可以使用此 URL https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal以 JSON 格式获取多伦多和蒙特利尔之间的路线

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

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

回答by dilettante

You should try this Driving route directions

你应该试试这个 行车路线