Java 使用谷歌地图API v2获取驾驶方向

Java 使用谷歌地图API v2获取驾驶方向,java,android,google-maps,google-maps-android-api-2,directions,Java,Android,Google Maps,Google Maps Android Api 2,Directions,我试图在两个位置之间获得驾驶方向: LatLng(12.917745600000000000,77.623788300000000000) LatLng(12.842056800000000000,7.663096499999940000) 我尝试过的代码: Polyline line = mMap.addPolyline(new PolylineOptions(). add(new LatLng(12.917745600000000000,77.623788300000000000)

我试图在两个位置之间获得驾驶方向:

LatLng(12.917745600000000000,77.623788300000000000)
LatLng(12.842056800000000000,7.663096499999940000)
我尝试过的代码:

Polyline line = mMap.addPolyline(new PolylineOptions().
    add(new LatLng(12.917745600000000000,77.623788300000000000),
    new LatLng(12.842056800000000000,7.663096499999940000))
       .width(5).color(Color.RED));
但这在两点之间画了一条直线。


有没有其他方法/途径可以获取这两点之间的驾驶方向。

我刚刚发布了Android上最新的谷歌地图方向API库
这就是我正在使用的

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("http://maps.google.com/maps?saddr="+latitude_cur+","+longitude_cur+"&daddr="+latitude+","+longitude));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_LAUNCHER );     
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);

您还可以尝试以下项目,以帮助使用该api。在这里:

它的工作原理,确切地说,很简单:

public class MainActivity extends ActionBarActivity implements DCACallBack{
/**
 * Get the Google Direction between mDevice location and the touched location using the     Walk
 * @param point
 */
private void getDirections(LatLng point) {
     GDirectionsApiUtils.getDirection(this, mDeviceLatlong, point,     GDirectionsApiUtils.MODE_WALKING);
}

/*
 * The callback
 * When the direction is built from the google server and parsed, this method is called and give you the expected direction
 */
@Override
public void onDirectionLoaded(List<GDirection> directions) {        
    // Display the direction or use the DirectionsApiUtils
    for(GDirection direction:directions) {
        Log.e("MainActivity", "onDirectionLoaded : Draw GDirections Called with path " + directions);
        GDirectionsApiUtils.drawGDirection(direction, mMap);
    }
}
public类MainActivity扩展了ActionBarActivity实现了DCACallBack{
/**
*使用“漫游”按钮获取mDevice位置和触摸位置之间的谷歌方向
*@param点
*/
专用方向(停车点){
GDirectionsApiUtils.getDirection(this,mDeviceLatlong,point,GDirectionsApiUtils.MODE_WALKING);
}
/*
*回调
*当从google服务器构建并解析方向时,将调用此方法并为您提供预期的方向
*/
@凌驾
公共void onDirectionLoaded(列表方向){
//显示方向或使用方向箭头
用于(GDDirection方向:方向){
Log.e(“MainActivity”,“onDirectionLoaded:使用路径“+方向”调用Draw GDDirections);
G方向Sapiutils.图纸D方向(方向,mMap);
}
}

旁注:您可能会丢失坐标末尾的一些数字。您已将位置指定为1/1000000000毫米,考虑到大陆漂移在每秒1/100000毫米的范围内,这使得坐标很快出现错误……请让我了解有关您的详细信息上面写的“示例代码”。在我的例子中,我无法得到多段线,只能得到一个简单的映射。我为Google Direction API@Akexorcist编写的最新代码通过提供的代码得到空指针异常错误消息。你知道吗?@Akexorcist嗨,我在nl1=doc.getElementsByTagName(“步骤”)得到一个空指针异常;在你的GMapV2Direction类中,你知道为什么吗?@AlbertoM发生了什么事?我在2个月前刚刚更新了我的库。这是一个web实现,我不认为这是他想要的。@Basim Sherri如果我尝试过这个方法,但当它打开地图应用程序时,起点总是被认为是设备的当前位置,即使我在“saddr”中提供不同的位置。你能帮助我如何避免这种情况吗?@Zeba你能发布你的代码吗?只是需要的意图部分。我的设备区域设置是德国当我在谷歌地图中显示路线时,它以德国单位显示经纬度(“,”而不是“.”).我们可以更改吗?我需要英文。@Zeba只需删除url中saddr之前的空格!