views:

4284

answers:

3

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!

+8  A: 

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

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

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

Max Gontar
how to handle the zoom level of the view.? i am working on android platform.
Praveen Chandrasekaran
http://code.google.com/intl/uk-UA/android/add-ons/google-apis/reference/com/google/android/maps/MapController.html#setZoom%28int%29
Max Gontar
A: 

Hi Max,

I'm really interested in this Route Provider project for my app, but If I look at the beginning of your post, it seems out of bounds with Google rules isn't it? I don't really want my app to be blocked... could you tell me if this is allowed or you you just offer a way around the rule ;) thanks

Sephy
Hi Sephy, it's like undocumented use of api's. And they have a right to lock your Google Map API Dev Key. Please use comments instead of answer in case you want to ask questions :)
Max Gontar
Ok then. I'll look for another way then, don't want my key locked...thx
Sephy
A: 

Hi! I have problems implementing this route provider. I want first to get current location via LocationListener, and after that to run the code that is in the thread. is this possible?

Thanks

Dino
this should be a small problem, implement LocationListener, and in onLocationChanged (android) or locationUpdated (blackberry) create routing thread (remember to set loacation var to final so it will be accessible from thread). also add some time limit so thread may be called once a 10 minutes or smt.
Max Gontar