使用Google Maps API v2获取驾车路线指南

55

我正在尝试获取两个位置之间的驾驶方向:

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

但这条线是直线连接两个点。

是否有其他方法/方式可以获取这两个点之间的驾车路线。


23
注:您可以忽略坐标末尾的一些数字。您已经将位置指定到了千分之一十亿毫米级别,这使得坐标很快就会变得不正确,考虑到板块漂移的范围为每秒1/100000毫米...... - Guffa
4个回答

150

如果您制作一个可以插入多个位置并找到最快路径并引导您穿过这些位置的程序,那将非常有效。这样做可以更快地访问多个位置,节省燃料和时间。 - Pierre
2
我最新的Google Direction API代码 https://github.com/akexorcist/Android-GoogleDirectionAndPlaceLibrary - Akexorcist
3
我遵循提供的代码,但出现了空指针异常错误消息。您有什么想法吗? - user2424370
2
@Akexorcist 你好,我在你的GMapV2Direction类中的 nl1 = doc.getElementsByTagName("step"); 处遇到了NullPointerException异常,你知道为什么吗? - Giorgio Antonioli
1
@AlbertoM,发生了什么事?我只是在两个月前更新了我的库。 - Akexorcist
显示剩余14条评论

17

这是我正在使用的内容,

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

2
这是一个网络实现,我不认为这是他想要的。 - yams
@Basim Sherif 我尝试了这个,但是当它打开地图应用程序时,起点始终被认为是设备的当前位置,即使我在 'saddr' 中提供了不同的位置。你能帮我解决一下吗? - Zeba
@Zeba,你能否请发一下你的代码?我只需要intent部分。 - Basim Sherif
@Basim Sherif:简单有效的解决方案,非常有帮助。我想关注您获取更多的安卓帮助。请分享您的Facebook ID。 - suri
1
@Zeba 在 URL 中删除 saddr 前面的空格! - Sjd
显示剩余2条评论

3
您可以尝试以下旨在帮助使用该API的项目。它在这里:https://github.com/MathiasSeguy-Android2EE/GDirectionsApiUtils 它的工作原理非常简单:
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);
    }
}

0

我找到了这个 -

val polyline1 = map?.addPolyline(
            PolylineOptions().clickable(true)
                .add(LatLng(23.8103, 90.4125), LatLng(4.0383, 21.7587))
                .geodesic(true)
        )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接