Android:自API 1.0以来已删除DrivingDirections - 如何在1.5 / 1.6中执行此操作?
-
06-07-2019 - |
题
自Android API 1.0以来,似乎已删除 DrivingDirections
现在有什么方法可以显示带有两个点的地图(其中一个可能是当前位置,但也可以是任何其他位置)以及Android 1.6中从一个点到另一个点的方向?
解决方案
我怀疑答案是从TeleNav或某人那里注册一张价值数百万美元的许可证。
之所以取消API,是因为谷歌本身对行车方向的限制(例如,无法进行实时转弯)会有限制,因为谷歌会从TeleNav或其他公司获得这些数据。反过来,谷歌不能让开发人员做那些受限制的事情,而且这种情况下开放的API无法得到充分的保护。
我以为我看到提到开放街道地图有行车路线,FWIW。
其他提示
如果您只需要与线连接的点,则不需要完整的kml。更快的方法是使用带有output = dragdir
的Google Maps API返回的JSONprivate 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文件,您可以使用String的split()轻松提取信息
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;
}
返回的String是折线,可以使用以下方法将其解码为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上或如何设置路由器类,请告诉我,我可以向您发送完整的代码。
可能有点太晚了,但对其他人仍然有帮助。 我已经编写了一些简单的类来获取和显示MapView for Android中的行车路线:
http://home.ameliemedia.com/android-app-aroundme/ #tips
希望它有所帮助!
安德烈。
Google目前的解决方案是使用 Google Maps API网络服务 。免费限量使用;你需要支付(成功的)商业用途。