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