J2ME/Android/BlackBerry-驾驶路线,两地之间的路线

在 Android 1.0上有一个 com.google.googlenav 名称空间,用于指示驾驶路线:
路线-改进的谷歌驾驶指南
但是在新的 SDK 中,由于某种原因它被删除了..。
Android: 自 API 1.0以来删除了 DrivingDirections-如何在1.5/1.6中实现? 在黑莓手机上也缺乏这类应用的 API:
如何在黑莓手机中找到两地之间的路线?

Csie-tw 提供了一个解决方案(针对 kml 文件的查询 gmap 并解析它) :
Android-驾驶方向(路线)
安德莉亚也为 Android 制作了 DrivingDirections 帮助类
我在 j2me 中为这个功能编写了一个小助手,所以我想在 Android 和 BlackBerry 上分享我的示例。

更新
正如评论中所说,谷歌地图 API 服务条款是不被官方允许的:

谷歌地图/谷歌地球 API 服务条款
最后更新: 2009年5月27日
...
10.执照限制。除非条款明确允许,或者除非你事先得到 Google 的书面授权(或者,如果适用的话,从特定内容的提供者那里) ,否则以上 Google 的许可将受到你遵守以下所有限制的限制。除非在第7节或地图 API 文档中明确允许,否则不得(也不得允许其他人) :
...
10.9使用本服务或内容的任何产品、系统或应用程序,或与以下内容有关的产品、系统或应用程序:
(a)实时导航或路线指引,包括但不限于与使用者的传感器装置的位置同步的逐路转弯路线指引;

并且可能对某些应用程序禁用(至少在 Android 上是这样) ... ... 来自 .NET 会话中的地理代码抓取:

这是 API 使用条款所不允许的。您不应该刮 谷歌地图生成地理编码。我们将阻止服务做 自动查询我们的服务器。

Bret Taylor
谷歌地图产品经理

如有任何选择和/或建议,将不胜感激!
谢谢!

106107 次浏览

J2ME Map Route Provider

maps.google.com has a navigation service which can provide you route information in KML format.

To get kml file we need to form url with start and destination locations:

public static String getUrl(double fromLat, double fromLon,
double toLat, double toLon) {// connect to map web service
StringBuffer urlString = new StringBuffer();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");// from
urlString.append(Double.toString(fromLat));
urlString.append(",");
urlString.append(Double.toString(fromLon));
urlString.append("&daddr=");// to
urlString.append(Double.toString(toLat));
urlString.append(",");
urlString.append(Double.toString(toLon));
urlString.append("&ie=UTF8&0&om=0&output=kml");
return urlString.toString();
}

Next you will need to parse xml (implemented with SAXParser) and fill data structures:

public class Point {
String mName;
String mDescription;
String mIconUrl;
double mLatitude;
double mLongitude;
}


public class Road {
public String mName;
public String mDescription;
public int mColor;
public int mWidth;
public double[][] mRoute = new double[][] {};
public Point[] mPoints = new Point[] {};
}

Network connection is implemented in different ways on Android and Blackberry, so you will have to first form url:

 public static String getUrl(double fromLat, double fromLon,
double toLat, double toLon)

then create connection with this url and get InputStream.
Then pass this InputStream and get parsed data structure:

 public static Road getRoute(InputStream is)

Full source code RoadProvider.java

BlackBerry

class MapPathScreen extends MainScreen {
MapControl map;
Road mRoad = new Road();
public MapPathScreen() {
double fromLat = 49.85, fromLon = 24.016667;
double toLat = 50.45, toLon = 30.523333;
String url = RoadProvider.getUrl(fromLat, fromLon, toLat, toLon);
InputStream is = getConnection(url);
mRoad = RoadProvider.getRoute(is);
map = new MapControl();
add(new LabelField(mRoad.mName));
add(new LabelField(mRoad.mDescription));
add(map);
}
protected void onUiEngineAttached(boolean attached) {
super.onUiEngineAttached(attached);
if (attached) {
map.drawPath(mRoad);
}
}
private InputStream getConnection(String url) {
HttpConnection urlConnection = null;
InputStream is = null;
try {
urlConnection = (HttpConnection) Connector.open(url);
urlConnection.setRequestMethod("GET");
is = urlConnection.openInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
}

See full code on J2MEMapRouteBlackBerryEx on Google Code

Android

Android G1 screenshot

public class MapRouteActivity extends MapActivity {
LinearLayout linearLayout;
MapView mapView;
private Road mRoad;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
new Thread() {
@Override
public void run() {
double fromLat = 49.85, fromLon = 24.016667;
double toLat = 50.45, toLon = 30.523333;
String url = RoadProvider
.getUrl(fromLat, fromLon, toLat, toLon);
InputStream is = getConnection(url);
mRoad = RoadProvider.getRoute(is);
mHandler.sendEmptyMessage(0);
}
}.start();
}


Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
TextView textView = (TextView) findViewById(R.id.description);
textView.setText(mRoad.mName + " " + mRoad.mDescription);
MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
};
};


private InputStream getConnection(String url) {
InputStream is = null;
try {
URLConnection conn = new URL(url).openConnection();
is = conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}

See full code on J2MEMapRouteAndroidEx on Google Code