LWUIT guide bee map development example: overlay custom Layers

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.
......
} So a simple method is to draw any custom image or image on the map after the map is drawn.
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.

[Java]
Package com. pstreets. gisengine. demo. lwuit;

// ------------------------------------- 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. MapDemoLWUIT;

Public class MapOverlayLWUIT extends MapDemoLWUIT {

OverLayMapLayer mapLayer;

Public void startApp (){
Init ();
Canvas. setTitle ("Map Overlay ");

GeoLatLng center = new GeoLatLng (32.0616667, 118.7777778 );
Map. setCenter (center, 9, MapType. MICROSOFTCHINA );
MapLayer = new OverLayMapLayer (canvas. getWidth (),
Canvas. getHeight ());
Map. addMapLayer (mapLayer );
Canvas. show ();
}

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 ){
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 );

}


}
}

Package com. pstreets. gisengine. demo. lwuit;
 
// ------------------------------------- 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. MapDemoLWUIT;
 
Public class MapOverlayLWUIT extends MapDemoLWUIT {
 
OverLayMapLayer mapLayer;

Public void startApp (){
Init ();
Canvas. setTitle ("Map Overlay ");

GeoLatLng center = new GeoLatLng (32.0616667, 118.7777778 );
Map. setCenter (center, 9, MapType. MICROSOFTCHINA );
MapLayer = new OverLayMapLayer (canvas. getWidth (),
Canvas. getHeight ());
Map. addMapLayer (mapLayer );
Canvas. show ();
}
 
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 ){
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 );
 
}
 

}
}

 
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.