- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 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?在 BlackBerry 上也缺少此类内容的 API:
how to find the route between two places in Blackberry?
csie-tw 给出了一个解决方法(为 kml 文件查询 gmaps 并解析它):
Android - Driving Direction (Route Path)
还有 Andrea做了一个DrivingDirections helper classes适用于 Android。
我在 j2me 中为此功能编写了一个小助手,因此我想在 Android 和黑莓上分享我的示例。
更新
正如评论中所述,官方不允许Google Maps APIs Terms of Service :
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;
并且某些应用程序可能会被禁用(不知何故,至少在 Android 上)... 来自 Geocode scraping in .NET conversation :
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
如有任何替代方案和/或建议,我们将不胜感激!
谢谢!
最佳答案
maps.google.com 有一个导航服务,可以为您提供路线信息 KML格式。
要获取 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();
}
接下来您将需要解析 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 上以不同的方式实现,因此您必须首先形成 url:
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;
}
}
请参阅 J2MEMapRouteBlackBerryEx 上的完整代码在谷歌代码上
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;
}
}
请参阅 J2MEMapRouteAndroidEx 上的完整代码在谷歌代码上
关于android - J2ME/Android/BlackBerry - 行车路线,两个地点之间的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5836108/
我是一名优秀的程序员,十分优秀!