在Android 1.0上,有一个com.google.googlenav命名空间用于行车路线:
Route - Improved Google Driving Directions
但是在较新的SDK中,它被某种原因删除了......
Android: DrivingDirections removed since API 1.0 - how to do it in 1.5/1.6?
在黑莓手机上也缺少这些东西的API:
how to find the route between two places in Blackberry?
csie-tw提供了一种解决方法(查询kml文件的gmaps并解析它):
Android - Driving Direction (Route Path)
此外,Andrea为Android制作了DrivingDirections helper classes
我在j2me中为这个功能写了一个小助手,所以我想在Android和BlackBerry上分享我的样本。
更新
正如评论中所述,它不是正式允许的Google Maps APIs Terms of Service :
Google地图/ Google地球API服务条款
最后更新时间:2009年5月27日
...
10.许可限制。除非条款明确允许,或者除非您事先获得Google(或适用的特定内容提供商)的书面授权,否则Google的上述许可均受您遵守以下所有限制的约束。除非在第7节或Maps API文档中明确允许,否则您不得(也不允许任何其他人):
...
10.9将服务或内容与任何产品,系统或应用程序一起使用或与之相关:
(a)实时导航或路线引导,包括但不限于与用户的传感器设备位置同步的转弯路线引导;
可能会针对某些应用停用(不知何故,至少在Android上)...来自Geocode scraping in .NET conversation:
API使用条款不允许这样做。你不应该刮 谷歌地图生成地理编码。我们将阻止这样做的服务 自动查询我们的服务器。
布雷特泰勒 产品经理,谷歌地图
对任何替代方案和/或建议表示感谢! 谢谢!
答案 0 :(得分:116)
maps.google.com有一个导航服务,可以KML格式为您提供路线信息。
要获取kml文件,我们需要形成包含起始位置和目标位置的网址:
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();
}
接下来,您将需要解析xml(使用SAXParser实现)并填充数据结构:
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[] {};
}
网络连接在Android和Blackberry上以不同方式实现,因此您必须首先形成网址:
public static String getUrl(double fromLat, double fromLon,
double toLat, double toLon)
然后创建与此url的连接并获取InputStream 然后传递此InputStream并获得解析的数据结构:
public static Road getRoute(InputStream is)
完整源代码RoadProvider.java
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;
}
}
查看Google代码{/ 3}上的完整代码
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;
}
}
查看Google代码{/ 3}上的完整代码