J2ME/Android/BlackBerry - 行车路线,两个地点之间的路线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2023669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
J2ME/Android/BlackBerry - driving directions, route between two locations
提问by Maksym Gontar
On Android 1.0 there was a com.google.googlenav namespace for driving directions:
Route - Improved Google Driving Directions
But in newer SDK it was removed by some reason...
Android: DrivingDirections removed since API 1.0 - how to do it in 1.5/1.6?On BlackBerry there is also lack of APIs for such stuff:
how to find the route between two places in Blackberry?
在 Android 1.0 上有一个 com.google.googlenav 行车
路线命名空间:路线 - 改进的谷歌行车路线
但在较新的 SDK 中它因某种原因被删除......
Android:自 API 1.0 以来删除了DrivingDirections - 如何在 1.5 中做到这一点/ 1.6?在黑莓上也缺乏这些东西的 API:
如何在黑莓中找到两个地方之间的路线?
csie-tw gives a workaround (query gmaps for kml file and parse it):
Android - Driving Direction (Route Path)
Also Andreamade a DrivingDirections helper classesfor Android.
I wrote a little helper for this functionality, in j2me, so I would like to share my samples on Android and BlackBerry.
csie-tw 提供了一种解决方法(查询 kml 文件的 gmaps 并对其进行解析):
Android - Driving Direction(路线路径)Andrea
还为 Android制作了一个DrivingDirections 辅助类。
我在 j2me 中为此功能编写了一个小助手,因此我想在 Android 和 BlackBerry 上分享我的示例。
UPDATE
As it was stated in comments, it's not officially allowed Google Maps APIs Terms of Service :
更新
正如评论中所述,谷歌地图 API 服务条款未被正式允许:
Google Maps/Google Earth APIs Terms of Service
Last updated: May 27, 2009
...
10. License Restrictions. Except as expressly permitted under the Terms, or unless you have received prior written authorization from Google (or, as applicable, from the provider of particular Content), Google's licenses above are subject to your adherence to all of the restrictions below. Except as explicitly permitted in Section 7 or the Maps APIs Documentation, you must not (nor may you permit anyone else to):
...
10.9 use the Service or Content with any products, systems, or applications for or in connection with:
(a) real time navigation or route guidance, including but not limited to turn-by-turn route guidance that is synchronized to the position of a user's sensor-enabled device;
Google Maps/Google Earth APIs 服务条款
最后更新日期:2009 年 5 月 27 日
...
10. 许可限制。除非条款明确允许,或者除非您事先获得 Google(或,如适用,特定内容的提供者)的书面授权,否则 Google 的上述许可以您遵守以下所有限制为前提。除非第 7 节或 Maps API 文档明确允许,否则您不得(也不得允许其他任何人):
...
10.9 将服务或内容与任何产品、系统或应用程序一起使用,用于或与以下目的相关:
(a) 实时导航或路线指引,包括但不限于与用户启用传感器的设备的位置同步的逐向路线指引;
and may be disabled for certain apps (somehow, at least on Android)... From Geocode scraping in .NET conversation:
并且可能会被某些应用程序禁用(不知何故,至少在 Android 上)......来自.NET 对话中的地理编码抓取:
This is not allowed by the API terms of use. You should not scrape Google Maps to generate geocodes. We will block services that do automated queries of our servers.
Bret Taylor
Product Manager, Google Maps
API 使用条款不允许这样做。您不应该通过抓取 Google 地图来生成地理编码。我们将阻止对我们的服务器进行自动查询的服务。
Bret Taylor
产品经理,Google 地图
Would be grateful for any alternatives and/or suggestions!
Thanks!
将不胜感激任何替代方案和/或建议!
谢谢!
回答by Maksym Gontar
J2ME Map Route Provider
J2ME 地图路由提供者
maps.google.com has a navigation service which can provide you route information in KMLformat.
maps.google.com 提供导航服务,可以为您提供KML格式的路线信息。
To get kml file we need to form url with start and destination locations:
要获取 kml 文件,我们需要使用起始位置和目标位置形成 url:
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:
接下来您需要解析 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[] {};
}
Network connection is implemented in different ways on Android and Blackberry, so you will have to first form url:
网络连接在 Android 和 Blackberry 上以不同的方式实现,因此您必须首先形成 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:
然后创建与此 url 的连接并获取 InputStream。
然后传递这个 InputStream 并获得解析的数据结构:
public static Road getRoute(InputStream is)
Full source code RoadProvider.java
完整源代码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 J2MEMapRouteBlackBerryExon Google Code
在 Google Code上查看J2MEMapRouteBlackBerryEx上的完整代码
Android
安卓
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 J2MEMapRouteAndroidExon Google Code
在 Google Code上查看J2MEMapRouteAndroidEx上的完整代码