使用 Google Maps API v2 获取行车路线

Posted

技术标签:

【中文标题】使用 Google Maps API v2 获取行车路线【英文标题】:Get driving directions using Google Maps API v2 【发布时间】:2013-01-07 20:04:21 【问题描述】:

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

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

但这会在两点之间画一条直线。 有没有其他方法/方式来获取这两点之间的行车路线。

【问题讨论】:

旁注:您可能会丢失坐标末尾的一些数字。考虑到大陆漂移在每秒 1/100000 毫米的范围内,您已将位置指定到 1/10000000000 毫米,这使得坐标错误非常快...... 【参考方案1】:

我刚刚发布了我最新的 android 上的 Google Maps Direction API 库 https://github.com/akexorcist/Android-GoogleDirectionLibrary

【讨论】:

请告诉我更多关于上面写的“示例代码”的信息。就我而言,我无法获得折线,只能获得一张简单的地图。 我最新的Google Direction API代码github.com/akexorcist/Android-GoogleDirectionAndPlaceLibrary @Akexorcist 我按照提供的代码收到空指针异常错误消息。你有什么想法吗? @Akexorcist 嗨,我在 nl1 = doc.getElementsByTagName("step"); 处收到 NullPointerException在你的 GMapV2Direction 类中,你知道为什么吗? @AlbertoM 这是怎么回事?我刚刚在 2 个月前更新了我的库。【参考方案2】:

这是我正在使用的,

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

【讨论】:

这是一个网络实现我不认为这是他想要的。 @Basim Sherif 我试过了,但是当它打开地图应用程序时,即使我在“saddr”中提供了不同的位置,起点始终被视为设备的当前位置。你能帮我如何避免这种情况吗? @Zeba 你能发布你的代码吗?只需要意图部分。 我的设备区域设置是德国,当我在 Google 地图中显示路线时,它以德国单位显示纬度经度(',' 而不是 '.')。我们可以更改吗?我需要英文版的。 @Zeba 只是删除 url 中 saddr 之前的空格!【参考方案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);
    

【讨论】:

以上是关于使用 Google Maps API v2 获取行车路线的主要内容,如果未能解决你的问题,请参考以下文章

使用地理编码器和 android Google Maps API v2 获取纬度和经度

如何使用字符串地址在 google maps API V2 android 中获取地理坐标?

Google Maps Android API v2 授权失败

Google Maps Android api v2 和当前位置

Android Google Maps API v2 显示路线 [重复]

Google Maps API v3和fusion图层:获取行数据