似乎DrivingDirections
已經由於Android API取出1.0Android:自API 1.0以來刪除DrivingDirections - 如何在1.5/1.6?
什麼現在是來展示具有兩個點的地圖(其中一人可能是當前位置,也可以是任意其他位置),並從方向在Android 1.6中一個到另一個?
似乎DrivingDirections
已經由於Android API取出1.0Android:自API 1.0以來刪除DrivingDirections - 如何在1.5/1.6?
什麼現在是來展示具有兩個點的地圖(其中一人可能是當前位置,也可以是任意其他位置),並從方向在Android 1.6中一個到另一個?
我懷疑答案是要從TeleNav或其他人那裏註冊數百萬美元的許可證。
API被拉的原因是因爲Google本身對駕車路線指令可以做的事情有限制(例如不能做實時轉彎的事情),因爲Google從TeleNav或其他公司。反過來,Google也不能讓開發人員執行那些受限制的事情,並且對於這種情況,開放的API無法得到充分的保護。
我以爲我看到提到開放街道地圖有駕駛方向,FWIW。
這可能有點太晚了,但仍然對其他人有幫助。 我已經寫了一些簡單的類來獲取和Android的顯示在圖形頁面行車路線:
http://home.ameliemedia.com/android-app-aroundme/#tips
希望它能幫助!
Andrea。
從谷歌目前的解決方案是使用Google Maps API Web Services。免費限制使用;你需要支付(成功)商業用途。
如果你只需要連接一條線的點,你不需要完整的kml。更快的方式做到這一點是隻使用JSON從輸出谷歌地圖API返回= dragdir
private String getUrl(String start, String end) {
//If params GeoPoint convert to lat,long string here
StringBuffer urlString = new StringBuffer();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");// from
urlString.append(start);
urlString.append("&daddr=");// to
urlString.append(end);
urlString.append("&ie=UTF8&0&om=0&output=dragdir"); //DRAGDIR RETURNS JSON
Log.i("URLString", urlString.toString());
return urlString.toString();
}
這urlString可用於使用字符串的分裂得到一個JSON文件,您可以輕鬆地提取信息()
private String getConnection(String url) {
URL inUrl = new URL(url);
URLConnection yc = inUrl.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
String encoded = "";
while ((inputLine = in.readLine()) != null)
encoded = encoded.concat(inputLine);
in.close();
String polyline = encoded.split("points:")[1].split(",")[0];
polyline = polyline.replace("\"", "");
polyline = polyline.replace("\\\\", "\\");
return polyline;
}
返回的字符串是其可被解碼成使用以下方法Geopoints的列表中的折線。
private static ArrayList<GeoPoint> decodePolyline(String encoded) {
ArrayList<GeoPoint> geopoints = new ArrayList<GeoPoint>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
GeoPoint p = new GeoPoint((int) (((double) lat/1E5) * 1E6), (int) (((double) lng/1E5) * 1E6));
geopoints.add(p);
}
return geopoints;
}
的最後一步是將這些點繪製到一個MapView,對於我們需要覆蓋的項目,將處理GeoPoints的ArrayList的。
public class PathOverlay extends Overlay {
private ArrayList<GeoPoint> pointList;
public PathOverlay(ArrayList<GeoPoint> pointList) {
this.pointList = pointList;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Point current = new Point();
Path path = new Path();
Projection projection = mapView.getProjection();
Iterator<GeoPoint> iterator = pointList.iterator();
if (iterator.hasNext()) {
projection.toPixels(iterator.next(), current);
path.moveTo((float) current.x, (float) current.y);
} else return;
while(iterator.hasNext()) {
projection.toPixels(iterator.next(), current);
path.lineTo((float) current.x, (float) current.y);
}
Paint pathPaint = new Paint();
pathPaint.setAntiAlias(true);
pathPaint.setStrokeWidth(3.0f);
pathPaint.setColor(Color.GREEN);
pathPaint.setStyle(Style.STROKE);
canvas.drawPath(path, pathPaint);
}
}
如果不能確定一些中間步驟,比如如何獲得覆蓋到的MapView或如何建立一個路由器類的讓我知道,我可以給你完整的代碼。
Andrea,該URL不提供示例代碼 - 似乎域註冊已過期。它可以在其他地方使用嗎? – 2012-06-28 12:27:21