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

时间:2013-01-24 06:10:16

标签: 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),
    new LatLng(12.842056800000000000,7.663096499999940000))
       .width(5).color(Color.RED));

但这在两点之间划了一条直线。

是否有其他方法/方法来获得这两点之间的行车路线。

3 个答案:

答案 0 :(得分:145)

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

答案 1 :(得分: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 :(得分: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);
    }
}