【问题标题】:Get driving directions using Google Maps API v2使用 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 毫米,这使得坐标错误非常快......

标签: java android google-maps google-maps-android-api-2 directions


【解决方案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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 2013-02-12
    • 1970-01-01
    • 2014-06-23
    相关资源
    最近更新 更多