Example of developing a map of Blackberry web bees: overlay a custom Layer

Source: Internet
Author: User

During application development, you often need to draw a point of user-defined interest or custom Ry on the map. The base provided by RasterMap in the Road Map package is an image.

[Java]
Protected void paint (Graphics g ){
Map. paint (mapGraphics );
G. drawImage (Image) mapImage. getNativeImage (), 0, 0, 0 );
// Start drawing your own sharps or images.
......
}

Protected void paint (Graphics g ){
Map. paint (mapGraphics );
G. drawImage (Image) mapImage. getNativeImage (), 0, 0, 0 );
// Start drawing your own sharps or images.
......
}
Therefore, a simple method is to draw any custom image or image on the map using any plotting method after drawing the map.
Here, we need to pay attention to coordinate transformation. RasterMap uses the longitude and latitude coordinates, while screen coordinates are used for screen display. RasterMap provides the method of Coordinate Conversion: fromScreenPixelToLatLng coordinates are converted to map longitude and latitude coordinates. FromLatLngToScreenPixel converts coordinates from longitude and latitude to screen coordinates.
The following example uses the method of deriving MapLayer subclass. RasterMap is a subclass of MapLayerContainer and can be used to manage multiple map layers. From bottom to top, these layers are equivalent to a layer of transparent paper stacked to form a final map.
The example shows several custom points of interest and a triangle, and the cross icon is displayed in the center of the map.

[Cpp]
// ----------------------------------- PACKAGE ------------------------------------
Package com. pstreets. gisengine. demo. rim;

// ------------------------------------- IMPORTS ------------------------------------
Import com. mapdigit. gis. MapLayer;
Import com. mapdigit. gis. drawing. IGraphics;
Import com. mapdigit. gis. geometry. GeoLatLng;
Import com. mapdigit. gis. geometry. GeoPoint;
Import com. mapdigit. gis. raster. MapType;
Import com. pstreets. gisengine. demo. MapDemoRIM;

// [-------------------------------- Main class ----------------------------------]
/**
* Map overlay demo for Guidebee Map API on RIM platform.
* <Hr> <B>©Copyright 2011 Guidebee, Inc. All Rights Reserved. </B>
* @ Version 1.00, 06/02/11
* @ Author Guidebee Pty Ltd.
*/
Public class MapOverlayRIM extends MapDemoRIM {

OverLayMapLayer mapLayer;
/**
* Entry point for application
* @ Param args Command line arguments (not used)
*/
Public static void main (String [] args)
{
// Create a new instance of the application and make the currently
// Running thread the application's event dispatch thread.
MapOverlayRIM theApp = new MapOverlayRIM ();
TheApp. enterEventDispatcher ();
}

Public MapOverlayRIM (){
Init ();
PushScreen (canvas );
GeoLatLng center = new GeoLatLng (32.0616667, 118.7777778 );
Map. setCenter (center, 10, MapType. GOOGLECHINA );
MapLayer = new OverLayMapLayer (canvas. getWidth (),
Canvas. getHeight ());
Map. addMapLayer (mapLayer );
}

Class OverLayMapLayer extends MapLayer {

GeoLatLng pt1 = new GeoLatLng (32.345281, 118.84261 );
GeoLatLng pt2 = new GeoLatLng (32.05899, 118.62789 );
GeoLatLng pt3 = new GeoLatLng (32.011811, 118.798656 );

Public OverLayMapLayer (int width, int height ){
Super (width, height );
}

Public void paint (IGraphics graphics, int offsetX, int offsetY ){
DrawCursor (graphics );
DrawTriangle (graphics );
DrawPoint (graphics, pt1 );
DrawPoint (graphics, pt2 );
DrawPoint (graphics, pt3 );

}

Public void drawTriangle (IGraphics g ){
GeoPoint ptOnScreen1 = map. fromLatLngToScreenPixel (pt1 );
GeoPoint ptOnScreen2 = map. fromLatLngToScreenPixel (pt2 );
GeoPoint ptOnScreen3 = map. fromLatLngToScreenPixel (pt3 );
G. setColor (0x0000FF );

G. drawLine (int) ptOnScreen1.x, (int) ptOnScreen1.y,
(Int) ptOnScreen2.x, (int) ptOnScreen2.y );
G. drawLine (int) ptOnScreen2.x, (int) ptOnScreen2.y,
(Int) ptOnScreen3.x, (int) ptOnScreen3.y );
G. drawLine (int) ptOnScreen1.x, (int) ptOnScreen1.y,
(Int) ptOnScreen3.x, (int) ptOnScreen3.y );
}

Public void drawPoint (IGraphics g, GeoLatLng pt ){
GeoPoint ptOnScreen = map. fromLatLngToScreenPixel (pt );
Int x = (int) ptOnScreen. x;
Int y = (int) ptOnScreen. y;
G. setColor (0x00FF00 );
G. fillRect (x-4, y-4, 8, 8 );

}

Private void drawCursor (IGraphics g ){
Int x = getMapWidth ()/2;
Int y = getMapHeight ()/2;
G. setColor (0x205020 );
G. drawRect (x-4, y-4, 8, 8 );
G. drawLine (x, y-6, x, y-2 );
G. drawLine (x, y + 6, x, y + 2 );
G. drawLine (x-6, y, x-2, y );
G. drawLine (x + 6, y, x + 2, y );
}
}

}

// ----------------------------------- PACKAGE ------------------------------------
Package com. pstreets. gisengine. demo. rim;
 
// ------------------------------------- IMPORTS ------------------------------------
Import com. mapdigit. gis. MapLayer;
Import com. mapdigit. gis. drawing. IGraphics;
Import com. mapdigit. gis. geometry. GeoLatLng;
Import com. mapdigit. gis. geometry. GeoPoint;
Import com. mapdigit. gis. raster. MapType;
Import com. pstreets. gisengine. demo. MapDemoRIM;
 
// [-------------------------------- Main class ----------------------------------]
/**
* Map overlay demo for Guidebee Map API on RIM platform.
* <Hr> <B>©Copyright 2011 Guidebee, Inc. All Rights Reserved. </B>
* @ Version 1.00, 06/02/11
* @ Author Guidebee Pty Ltd.
*/
Public class MapOverlayRIM extends MapDemoRIM {
 
OverLayMapLayer mapLayer;
/**
* Entry point for application
* @ Param args Command line arguments (not used)
*/
Public static void main (String [] args)
{
// Create a new instance of the application and make the currently
// Running thread the application's event dispatch thread.
MapOverlayRIM theApp = new MapOverlayRIM ();
TheApp. enterEventDispatcher ();
}
 
Public MapOverlayRIM (){
Init ();
PushScreen (canvas );
GeoLatLng center = new GeoLatLng (32.0616667, 118.7777778 );
Map. setCenter (center, 10, MapType. GOOGLECHINA );
MapLayer = new OverLayMapLayer (canvas. getWidth (),
Canvas. getHeight ());
Map. addMapLayer (mapLayer );
}
 
Class OverLayMapLayer extends MapLayer {
 
GeoLatLng pt1 = new GeoLatLng (32.345281, 118.84261 );
GeoLatLng pt2 = new GeoLatLng (32.05899, 118.62789 );
GeoLatLng pt3 = new GeoLatLng (32.011811, 118.798656 );
 
Public OverLayMapLayer (int width, int height ){
Super (width, height );
}
 
Public void paint (IGraphics graphics, int offsetX, int offsetY ){
DrawCursor (graphics );
DrawTriangle (graphics );
DrawPoint (graphics, pt1 );
DrawPoint (graphics, pt2 );
DrawPoint (graphics, pt3 );
 
}
 
Public void drawTriangle (IGraphics g ){
GeoPoint ptOnScreen1 = map. fromLatLngToScreenPixel (pt1 );
GeoPoint ptOnScreen2 = map. fromLatLngToScreenPixel (pt2 );
GeoPoint ptOnScreen3 = map. fromLatLngToScreenPixel (pt3 );
G. setColor (0x0000FF );
 
G. drawLine (int) ptOnScreen1.x, (int) ptOnScreen1.y,
(Int) ptOnScreen2.x, (int) ptOnScreen2.y );
G. drawLine (int) ptOnScreen2.x, (int) ptOnScreen2.y,
(Int) ptOnScreen3.x, (int) ptOnScreen3.y );
G. drawLine (int) ptOnScreen1.x, (int) ptOnScreen1.y,
(Int) ptOnScreen3.x, (int) ptOnScreen3.y );
}
 
Public void drawPoint (IGraphics g, GeoLatLng pt ){
GeoPoint ptOnScreen = map. fromLatLngToScreenPixel (pt );
Int x = (int) ptOnScreen. x;
Int y = (int) ptOnScreen. y;
G. setColor (0x00FF00 );
G. fillRect (x-4, y-4, 8, 8 );
 
}
 
Private void drawCursor (IGraphics g ){
Int x = getMapWidth ()/2;
Int y = getMapHeight ()/2;
G. setColor (0x205020 );
G. drawRect (x-4, y-4, 8, 8 );
G. drawLine (x, y-6, x, y-2 );
G. drawLine (x, y + 6, x, y + 2 );
G. drawLine (x-6, y, x-2, y );
G. drawLine (x + 6, y, x + 2, y );
}
}
 
}

Author: mapdigit
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.