J2ME /安卓/黑莓 - 行车路线,两个地点之间路由 [英] J2ME/Android/BlackBerry - driving directions, route between two locations

查看:177
本文介绍了J2ME /安卓/黑莓 - 行车路线,两个地点之间路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android 1.0有一个com.google.googlenav命名空间行车路线:
路线 - 改进谷歌行车路线
但是,在新的SDK它由于某种原因被删除...
<一href="http://stackoverflow.com/questions/1612533/android-drivingdirections-removed-since-api-1-0-how-to-do-it-in-1-5-1-6">Android: DrivingDirections因为删除API 1.0 - 如何做到这一点在1.5 / 1.6 黑莓也有不足的API这样的东西:
<一href="http://stackoverflow.com/questions/1542252/how-to-find-the-route-between-two-places-in-blackberry">how找到黑莓两地之间的路径?

CSIE-TW给出了一个解决方法(查询GMaps实现了KML文件,并解析它):
机器人 - 驾驶方向(路路)
此外安德烈做了的 DrivingDirections助手类的为Android。
我写了一个小帮手此功能,在J2ME中,所以我想和大家分享我的样品在Android和黑莓手机。

更新
因为它是在注释中规定,它不是正式允许谷歌地图API服务条款

  

谷歌地图/谷歌地球API服务条款
  最近更新日期:5月27日,2009年
  ...
  10.许可限制。除EX $ P $根据本条款pssly允许,或除非你已经(从特定的内容提供者或(如适用))收到书面授权,谷歌,谷歌的牌照以上内容如有您遵守以下所有限制。除第7或地图API文档明确允许,您不得(也不得允许任何其他人):
  ...
  10.9使用本服务或内容的任何产品,系统,或用于或与连接的应用程序:
  (一)实时导航或路径引导,包括但不限于转由转弯路径引导该被同步到用户的具备传感器能力的设备的位置;

和可能会禁用某些应用程序(不知何故,至少在Android)......从在.NET会话地理code刮:

  

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

     

布雷特·泰勒
  产品经理,谷歌地图

将不胜感激任何替代和/或建议!
谢谢!

解决方案

J2ME地图路线提供商

maps.google.com有一个导航服务,可以在的 KML 格式。

要获得KML文件,我们需要形成URL以起始位置和目标位置:

 公共静态字符串的getURL(双fromLat,双fromLon,
   双toLat,双托隆){//连接到地图Web服务
  StringBuffer的urlString =新的StringBuffer();
  urlString.append(http://maps.google.com/maps?f=d&hl=en);
  urlString.append(&放大器; SADDR =); //从
  urlString.append(Double.toString(fromLat));
  urlString.append(,);
  urlString.append(Double.toString(fromLon));
  urlString.append(&放大器; DADDR =); //给
  urlString.append(Double.toString(toLat));
  urlString.append(,);
  urlString.append(Double.toString(托隆));
  urlString.append(&放大器,即= UTF8&功放; 0安培; OM = 0&放大器;输出= KML);
  返回urlString.toString();
 }
 

接下来,您将需要解析XML(与SAXParser的实现),并填写数据结构:

 公共类点{
 字符串MNAME;
 字符串mDescription;
 字符串mIconUrl;
 双mLatitude;
 双mLongitude;
}


公共类路{
 公共字符串MNAME;
 公共字符串mDescription;
 公众诠释mColor;
 公众诠释mWidth;
 公共双[] [] mRoute =新的双[] [] {};
 公共点[] M点=新点[] {};
}
 

网络连接,在Android和黑莓不同的方式实现,所以你必须先表单网址:

 公共静态字符串的getURL(双fromLat,双fromLon,
   双toLat,双托隆)
 

然后创建与此URL连接,并获得InputStream的。照片 然后通过这个InputStream和被解析的数据结构:

 公共静态路getRoute(InputStream的是)
 

完整的源$ C ​​$ C <一个href="http://$c$c.google.com/p/j2memaprouteprovider/source/browse/trunk/J2MEMapRouteBlackBerryEx/src/org/ci/geo/route/RoadProvider.java"相对=nofollow> RoadProvider.java

黑莓

  

死链接 - 黑莓Storm截图

 类MapPathScreen扩展MainScreen {
 地图控件地图;
 路mRoad =新路();
 公共MapPathScreen(){
  双fromLat = 49.85,fromLon = 24.016667;
  双toLat = 50.45,托隆= 30.523333;
  字符串URL = RoadProvider.getUrl(fromLat,fromLon,toLat,托隆);
  InputStream的是=的getConnection(URL);
  mRoad = RoadProvider.getRoute(是);
  地图=新地图控件();
  添加(新的LabelField(mRoad.mName));
  添加(新的LabelField(mRoad.mDescription));
  加(图)
 }
 保护无效onUiEngineAttached(布尔附后){
  super.onUiEngineAttached(附后);
  如果(附后){
   map.drawPath(mRoad);
  }
 }
 私人的InputStream的getConnection(字符串URL){
  的HttpConnection的URLConnection = NULL;
  InputStream的是= NULL;
  尝试 {
   的URLConnection =(HttpConnection的)Connector.open(URL);
   urlConnection.setRequestMethod(GET);
   是= urlConnection.openInputStream();
  }赶上(IOException异常E){
   e.printStackTrace();
  }
  回报;
 }
}
 

请参阅<全code href="http://$c$c.google.com/p/j2memaprouteprovider/source/browse/#svn/trunk/J2MEMapRouteBlackBerryEx"相对=nofollow> J2MEMapRouteBlackBerryEx 在谷歌code

的Andr​​oid

 公共类MapRouteActivity扩展MapActivity {
 LinearLayout中的LinearLayout;
 图形页面图形页面;
 私家路mRoad;
 @覆盖
 公共无效的onCreate(包savedInstanceState){
  super.onCreate(savedInstanceState);
  的setContentView(R.layout.main);
  图形页面=(图形页面)findViewById(R.id.mapview);
  mapView.setBuiltInZoomControls(真正的);
  新的Thread(){
   @覆盖
   公共无效的run(){
    双fromLat = 49.85,fromLon = 24.016667;
    双toLat = 50.45,托隆= 30.523333;
    字符串URL = RoadProvider
      .getUrl(fromLat,fromLon,toLat,托隆);
    InputStream的是=的getConnection(URL);
    mRoad = RoadProvider.getRoute(是);
    mHandler.sendEmptyMessage(0);
   }
  }。开始();
 }

 处理器mHandler =新的处理程序(){
  公共无效的handleMessage(android.os.Message味精){
   TextView中的TextView =(TextView中)findViewById(R.id.description);
   textView.setText(mRoad.mName ++ mRoad.mDescription);
   MapOverlay mapOverlay =新MapOverlay(mRoad,图形页面);
   名单&LT;覆盖&GT; listOfOverlays =调用MapView.getOverlays();
   listOfOverlays.clear();
   listOfOverlays.add(mapOverlay);
   mapView.invalidate();
  };
 };

 私人的InputStream的getConnection(字符串URL){
  InputStream的是= NULL;
  尝试 {
   URLConnection的康恩=新的网址(URL).openConnection();
   是= conn.getInputStream();
  }赶上(MalformedURLException异常E){
   e.printStackTrace();
  }赶上(IOException异常E){
   e.printStackTrace();
  }
  回报;
 }
 @覆盖
 保护的布尔isRouteDisplayed(){
  返回false;
 }
}
 

查看全部code在 J2MEMapRouteAndroidEx 对谷歌code

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?

csie-tw gives a workaround (query gmaps for kml file and parse it):
Android - Driving Direction (Route Path)
Also Andrea made a DrivingDirections helper classes for Android.
I wrote a little helper for this functionality, in j2me, so I would like to share my samples on Android and BlackBerry.

UPDATE
As it was stated in comments, it's not officially allowed 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;

and may be disabled for certain apps (somehow, at least on Android)... From 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

Would be grateful for any alternatives and/or suggestions!
Thanks!

解决方案

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

Dead link - BlackBerry Storm screenshot

    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

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

这篇关于J2ME /安卓/黑莓 - 行车路线,两个地点之间路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆