Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
Posts: 53
Registered: ‎06-29-2011
My Device: Blackberry 9800
My Carrier: o2

display route on map from location A to B Blackberry

Hi all,

 

could someone please tell me how to display a route from location A to location B on a Map Field or RichMapfield.

 

 

BR,
Supriya
Regular Contributor
Posts: 53
Registered: ‎06-29-2011
My Device: Blackberry 9800
My Carrier: o2

Re: display route on map from location A to B Blackberry

Hi all,

 

this is my attempt of trying to draw a route from  A to B, i am just getting a single dot on the map, could someone else what is going wrong:

 

class MapPathScreen extends MainScreen {
    MapControl map;
    Road mRoad = new Road();
    RichMapField mapField = MapFactory.getInstance().generateRichMapField();

    public MapPathScreen() {
       double fromLat = 47.67, fromLon = 9.38, toLat =47.12, toLon = 9.47;
         String url = RoadProvider.getUrl(fromLat, fromLon, toLat, toLon);
         InputStream is = getConnection(url);
         mRoad = RoadProvider.getRoute(is);
         map = new MapControl(mapField);
         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;
    }
       protected boolean keyDown(int keycode, int time)
        {
           MapAction action=mapField.getAction();
            StringBuffer sb = new StringBuffer();
           
            // Retrieve the characters mapped to the keycode for the current keyboard layout
            Keypad.getKeyChars(keycode, sb);       
           
            // Zoom in
            if(sb.toString().indexOf('i') != -1)
            {
                action.zoomIn();
                return true;
            }
            // Zoom out
            else if(sb.toString().indexOf('o') != -1)
            {
                action.zoomOut();
                return true;
            }       

            return super.keyDown(keycode, time);
        }

}

class MapControl extends net.rim.device.api.lbs.maps.ui.MapField {
    Bitmap bmp = null;
    MapAction action;
    MapField map = new MapField();
    RichMapField mapRich;
   
    Road road;
    public MapControl(RichMapField mapRich)
    {
        this.mapRich = mapRich;
       
    }

   
    public void drawPath(Road road) {
   
        if (road.mRoute.length > 0) {
            Coordinates[] mPoints = new Coordinates[] {};
          
            for (int i = 0; i < road.mRoute.length; i++) {
                    Arrays.add(mPoints, new Coordinates(road.mRoute[i][1],
                                    road.mRoute[i][0], 0));
            }

            double moveToLat = mPoints[0].getLatitude()
                            + (mPoints[mPoints.length - 1].getLatitude() - mPoints[0]
                                            .getLatitude()) / 2;
            double moveToLong = mPoints[0].getLongitude()
                            + (mPoints[mPoints.length - 1].getLongitude() - mPoints[0]
                                            .getLongitude()) / 2;
            Coordinates moveTo = new Coordinates(moveToLat, moveToLong, 0);
      
            action = this.getAction();
           
            action.setZoom(15);
             action.setCentreAndZoom(new MapPoint(moveToLat,moveToLong), 15);

            bmp = new Bitmap(500, 500);
            bmp.createAlpha(Bitmap.ALPHA_BITDEPTH_8BPP);
            Graphics g = Graphics.create(bmp);
         
            int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
                      
            XYPoint point = new XYPoint();
                      
            Coordinates c  = new Coordinates(mPoints[0].getLatitude(),mPoints[0].getLongitude(),0);
            map.convertWorldToField(c, point);
            x1=point.x;
            y1 = point.y;
            g.fillEllipse(x1, y1, x1, y1 + 1, x1 + 1, y1, 0, 360);
           
            for (int i = 1; i < mPoints.length; i++) {
                    XYPoint point1 = new XYPoint();
                    Coordinates c1  = new Coordinates(mPoints[i].getLatitude(),mPoints[i].getLongitude(),0);
                    map.convertWorldToField(c1, point1);
                    x2 = point1.x;
                    y2 = point1.y;
                    g.setColor(Color.GREEN);
                    //g.fillEllipse(x1, y1, x1, y1 + 1, x1 + 1, y1, 0, 360);
                    g.drawLine(x1, y1, x2, y2);
                    x1 = x2;
                    y1 = y2;
            }
    }

    }

    protected void paint(Graphics g) {
        super.paint(g);
        if (bmp != null) {
            g.setGlobalAlpha(100);
            g.drawBitmap(0, 0, bmp.getWidth(), bmp.getHeight(), bmp, 0, 0);
        }
         

    }
    

 

BR,
Supriya
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: display route on map from location A to B Blackberry


supriya_dinakaran wrote:

Hi all,

 

could someone please tell me how to display a route from location A to location B on a Map Field or RichMapfield.

 

 



that's really easy done using a Location Document - you only need two coordinates of start and end point and invoke the Map App using this document :

 

MapsArguments mapsArgs = new MapsArguments(
	MapsArguments.ARG_LOCATION_DOCUMENT, 
	Utility.createRoutingDocument(
			null, // id, 
			coordinates[start], // fromCoordinate, 
			coordinates[destination], // toCoordinate, 
			null, // startlabel, 
			null, // destinationLabel, 
			true) //clearAllRoutes);
	);
	Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, mapsArgs);

 

and here's a utility to create such a document - but it's a XML doc so you can also use XML tools to do this

private static String LBS_DOCUMENT_START = "<lbs";
private static String LBS_DOCUMENT_HEADER_END = ">";
private static String LBS_DOCUMENT_END = "</lbs>";
private static String LBS_ID = "id";
private static String LBS_CLEAR = "clear";
private static String LBS_CLEAR_ALL = "ALL";
private static String LBS_CLEAR_ALL_ROUTES = "ROUTES";
private static String LBS_ROUTE_BEGIN = "<getRoute>";
private static String LBS_ROUTE_END = "</getRoute>";
private static String LBS_LOACTION_START = "<location";
private static String LBS_LOCATION_END = "/>";
private static String LBS_LAT = "lat"; // y integer (double * 100,000)
private static String LBS_LON = "lon"; // x integer (double * 100,000)
private static String LBS_LABEL = "label"; // String label
private static String LBS_DESCRIPTION = "description"; // String description
private static String LBS_CATEGORIES = "categories"; // String Category
private static String LBS_ADDRESS = "address"; //String street
private static String LBS_CITY = "city"; // String city
private static String LBS_POSTAL_CODE = "postalCode"; // String zip
private static String LBS_COUNTRY = "country"; // String country
private static String LBS_REGION = "region"; // String province or state
private static String LBS_EMAIL = "email"; // String email
private static String LBS_FAX = "fax"; // String fax
private static String LBS_PHONE = "phone"; // String phone
private static String LBS_URL = "url"; // String url
private static String LBS_RATING = "rating"; // double rating 0...5
private static String LBS_ZOOM = "zoom"; // integer 0...15

/**
 * creates a Location Document
 * Attention: does not support UTF-8
 * 
 * @param id
 * @param fromCoordinate
 * @param toCoordinate
 * @param startlabel
 * @param destinationLabel
 * @param clearAllRoutes
 * @return
 */							
public static String createRoutingDocument(
					String id,
					Coordinates fromCoordinate, 
					Coordinates toCoordinate, 
					String startlabel, 
					String destinationLabel, 
					boolean clearAllRoutes){
if (fromCoordinate == null || toCoordinate == null) {
	return "";
}
StringBuffer documentBuffer = new StringBuffer();
documentBuffer.append(LBS_DOCUMENT_START);
documentBuffer.append(Characters.SPACE);
if (clearAllRoutes) {
	documentBuffer.append(LBS_CLEAR);
	documentBuffer.append(Characters.EQUALS_SIGN);
	documentBuffer.append(Characters.APOSTROPHE);
	documentBuffer.append(LBS_CLEAR_ALL_ROUTES);
	documentBuffer.append(Characters.APOSTROPHE);
	documentBuffer.append(Characters.SPACE);
}
documentBuffer.append(LBS_ID);
documentBuffer.append(Characters.EQUALS_SIGN);
documentBuffer.append(Characters.APOSTROPHE);
if (id != null && id.length() > 0) {
	documentBuffer.append(id);
} else {
	documentBuffer.append("ROUTE");
}
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(Characters.SPACE);
documentBuffer.append(LBS_DOCUMENT_HEADER_END);
documentBuffer.append(Characters.SPACE);
// create route
documentBuffer.append(LBS_ROUTE_BEGIN);
documentBuffer.append(Characters.SPACE);
// route starts here
documentBuffer.append(LBS_LOACTION_START);
documentBuffer.append(Characters.SPACE);
// lat, lon, label, description are required
documentBuffer.append(LBS_LAT);
documentBuffer.append(Characters.EQUALS_SIGN);
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(MathUtilities.round(fromCoordinate.getLatitude()*100000));
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(Characters.SPACE);
documentBuffer.append(LBS_LON);
documentBuffer.append(Characters.EQUALS_SIGN);
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(MathUtilities.round(fromCoordinate.getLongitude()*100000));
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(Characters.SPACE);
documentBuffer.append(LBS_LABEL);
documentBuffer.append(Characters.EQUALS_SIGN);
documentBuffer.append(Characters.APOSTROPHE);
if (startlabel != null && startlabel.length() > 0) {
	documentBuffer.append(startlabel);
} else {
	documentBuffer.append("START"); // TODO
}
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(Characters.SPACE);
documentBuffer.append(LBS_DESCRIPTION);
documentBuffer.append(Characters.EQUALS_SIGN);
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append("here begins the calculated route"); // TODO
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(Characters.SPACE);
documentBuffer.append(LBS_LOCATION_END);
documentBuffer.append(Characters.SPACE);
// route ends here
documentBuffer.append(LBS_LOACTION_START);
documentBuffer.append(Characters.SPACE);
// lat, lon, label, description are required
documentBuffer.append(LBS_LAT);
documentBuffer.append(Characters.EQUALS_SIGN);
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(MathUtilities.round(toCoordinate.getLatitude()*100000));
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(Characters.SPACE);
documentBuffer.append(LBS_LON);
documentBuffer.append(Characters.EQUALS_SIGN);
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(MathUtilities.round(toCoordinate.getLongitude()*100000));
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(Characters.SPACE);
documentBuffer.append(LBS_LABEL);
documentBuffer.append(Characters.EQUALS_SIGN);
documentBuffer.append(Characters.APOSTROPHE);
if (destinationLabel != null && destinationLabel.length() > 0) {
	documentBuffer.append(destinationLabel);
} else {
	documentBuffer.append("DESTINATION"); // TODO
}
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(Characters.SPACE);
documentBuffer.append(LBS_DESCRIPTION);
documentBuffer.append(Characters.EQUALS_SIGN);
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append("here ends the calculated route"); // TODO
documentBuffer.append(Characters.APOSTROPHE);
documentBuffer.append(Characters.SPACE);
documentBuffer.append(LBS_LOCATION_END);
documentBuffer.append(Characters.SPACE);
documentBuffer.append(LBS_ROUTE_END);
documentBuffer.append(Characters.SPACE);
documentBuffer.append(LBS_DOCUMENT_END);
return documentBuffer.toString();
}

 hope it helps

BTW: just wrote a (german) article about this for next issue of mobile developer magazine

and also will start soon a blog series about GPS, KML, Geodata, LBS, MapField, RichMapField, Routes, Tracks and more at my blog

 

ekke

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: display route on map from location A to B Blackberry

reading your 2nd post:

 

you don't want to get a Route from BlackBerry Routing Server,

you have a collection of Waypoints and want to display the tracked path

 

this can easiest be done using a KML document: draw a LineString fom your Coordinates and you'll get the tracked path as a line on your RichMapField or invoke the Map App.

 

(there's one bug: the color of the line was lost on RichMapField but displayed correct on Map App)

 

if you also want to show all single wayPoints addionally you can add the MapLocation's to your RichMapField or add Landmark[] to Map App. (I'm not using a Location Dodument for this because in my cases I have UTF-8 with umlauts causing problems. Issue already added. 

 

as already told you, I'll blog soon about all of this

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Regular Contributor
Posts: 53
Registered: ‎06-29-2011
My Device: Blackberry 9800
My Carrier: o2

Re: display route on map from location A to B Blackberry

Thanks alot ekke for the reply.

 

But unfortunately am looking to display the route on a RichMapField.

The example code you have provided is to invoke Blackberry Maps.

 

In my application am already using the RichMapField to show the multiple locations.

i am using BB OS 6.

 

Please do let me know if i could do something else.

BR,
Supriya
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: display route on map from location A to B Blackberry

already answered in my other reply ;-)

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Regular Contributor
Posts: 53
Registered: ‎06-29-2011
My Device: Blackberry 9800
My Carrier: o2

Re: display route on map from location A to B Blackberry

sorry i don't get you.
How can i achieve this:
"this can easiest be done using a KML document: draw a LineString fom your Coordinates and you'll get the tracked path as a line on your RichMapField or invoke the Map App."

How will i get KML document? you mean like the way i get it from the code i gave above?
if so, how can i get the tracked path on the RichMap Field
BR,
Supriya
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: display route on map from location A to B Blackberry

[ Edited ]

supriya_dinakaran wrote:
sorry i don't get you.
How can i achieve this:
"this can easiest be done using a KML document: draw a LineString fom your Coordinates and you'll get the tracked path as a line on your RichMapField or invoke the Map App."

How will i get KML document? you mean like the way i get it from the code i gave above?
if so, how can i get the tracked path on the RichMap Field


take a look at the documentation how kml documents are used by BlackBerry

and at Google where you find description how to define a KML document

take a look at LineString

 

you need to have a server, because kml documents has to be used using an URL

creation of KML documents can be done easy on-the-fly dynamically

 

---

will post both server-code and bb-code on my blog next days

 

too much for a message here (and I'm under pressure to finish a customer project ;-) ...of course using all these things 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Regular Contributor
Posts: 53
Registered: ‎06-29-2011
My Device: Blackberry 9800
My Carrier: o2

Re: display route on map from location A to B Blackberry

Thanks alot ekke, looking forward to your blog.

 

Sorry to disturb you so many times, i am pretty new to Blackberry (hardly a month in).

 

I am trying to understand somethings. Guess samples will make me understand better.

 

Could you please give me a sample code of how i can draw the tracked path on a RichMapField, or even obtain a kml document.

BR,
Supriya
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: display route on map from location A to B Blackberry

[ Edited ]

populating your RichMap from KML is easy:

 

// blocks needs extra Thread
Vector errorVector = MapFactory.getInstance()
		.populateDataModelFromKmlUrl(
				data, 
				theUrl,
				TAG_TRACKED_PATH);
if (errorVector != null && errorVector.size() > 0) {
	errorFromKml(errorVector);
	return;
}

 

there's no file because I do it this way:

call my server sending the coordinates as get parameters

and server responds kml dynamically generated from coordinates

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner