J2Me/Android/Blackberry - Driving Directions, Route Between Two Locations

J2ME/Android/BlackBerry - driving directions, route between two locations

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

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

Android show driving direction route between two geopoints

Hey guys The Problem in the Link I found it

Google has stopped to give the api response in KML format

also the solution for the same is here

How to find the route between two places in BlackBerry?

As I understood, you need to get a path length between several locations.

It's not possible in MapField, you can only set that route by yourself.

Invoke BB Maps for route direction

You can always use MapField in you application for simple functionality, like present locations, and use BlackBerry Maps application for enhanced stuff like select locations and route directions presentation for user.

To invoke BB Maps for route direction use code:

Invoke.invokeApplication(Invoke.APP_TYPE_MAPS,
new MapsArguments(
MapsArguments.ARG_LOCATION_DOCUMENT,document));

With document formed like:

<location-document>
<GetRoute>
<location lon='-8030000' lat='4326000' label='Kitchener, ON' description='Kitchener, Ontario, Canada' />
<location lon='-7569792' lat='4542349' label='Ottawa, ON' description='Ottawa, Ontario, Canada' />
</GetRoute>
</location-document>

Result will be like

alt text http://www.blackberry.com/knowledgecentersupport/kmsupport/supportknowledgebase/images/DB-00599_3.jpgalt text http://www.blackberry.com/knowledgecentersupport/kmsupport/supportknowledgebase/images/DB-00599_4.jpg

Now, in BlackBerry Maps you can register your App menu item and start or update your App with selected location using MapView.

See How To - Add an ApplicationMenuItem to BlackBerry Maps

UPDATE

Use the gmap navigation service for driving directions

See J2ME/Android/BlackBerry - driving directions, route between two locations

How to draw a route between two geopoints on the Android

This application is contained with source code... It'll solve ur probs.

http://www.anddev.org/the_friend_finder_-_mapactivity_using_gps_-_part_i_-_ii-t93.html

Android route between two locations on google maps

I have solved this issue. Driving directions are not available for Turkey. But when I paste this link

http://maps.google.com/?q=from+Istanbul,Turkey+to+Ankara,Turkey&output=kml
to my browser it generates a kml file and automatically downloaded to your pc. Then you can add it into your project under res/raw folder and parse the elements.

How to display a route between two geocoords in google maps?

this is working example link. check it out. it helps to create the route overlay on the map. here is the complete source code for that.

Blackberry j2me location services

use this code

public class handleGPS{
static GPSThread gpsThread;
public static double latitude ;
public static double longitude;

public handleGPS(){
gpsThread = new GPSThread();
gpsThread.start();
}

private static class GPSThread extends Thread{
public void run() {
Criteria myCriteria = new Criteria();
myCriteria.setCostAllowed(false);

try {
LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria);

try {
Location myLocation = myLocationProvider.getLocation(300);
latitude = myLocation.getQualifiedCoordinates().getLatitude();
longitude = myLocation.getQualifiedCoordinates().getLongitude();
System.out.print("latitude= "+latitude+" longitude="+longitude);


}
catch ( InterruptedException iex ) {
return;
}
catch ( LocationException lex ) {
return;
}
}catch ( LocationException lex ) {
return;
}
return;
}
}
}

then on your main class, call the above class

    handleGPS handleGPS=new handleGPS();
int m_bbHandle = CodeModuleManager.getModuleHandle("net_rim_bb_lbs");
if(m_bbHandle>0){

Dialog.alert("GPS not found");
}
else{
Dialog.alert("GPS found");
//your code
}

driving direction between current location to a given point

Here is the answer. Just add the classes that is given there and call the required methods and it is done.



Related Topics



Leave a reply



Submit