簡體   English   中英

使用 Google 地圖 API v2 獲取行車路線

[英]Get driving directions using Google Maps API v2

我正在嘗試獲取兩個位置之間的行駛方向:

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上發布了我最新的Google Maps Direction API庫https://github.com/akexorcist/Android-GoogleDirectionLibrary

這是我正在使用的,

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的項目。 它在這里: 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);
    }
}

我發現了這個-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM