net.landspurg.map
Class MapCanvas

java.lang.Object
  extended by javax.microedition.lcdui.Displayable
      extended by javax.microedition.lcdui.Canvas
          extended by javax.microedition.lcdui.game.GameCanvas
              extended by net.landspurg.map.MapCanvas
All Implemented Interfaces:
java.lang.Runnable, BufferImageListener, OneLocListener
Direct Known Subclasses:
GPSDisplay

public class MapCanvas
extends javax.microedition.lcdui.game.GameCanvas
implements java.lang.Runnable, OneLocListener, BufferImageListener

This is the main interface to display a Map from Google...
The basic usage is quite simple. Just create a mapCanvas and display it...
Extended usage include usage of marker ( element of type oneLoc)
In the following example, a canvas is created with two markers, and the display is centered around it...
MIDP1.1 version

      mapCanvas c=new mapCanvas();
c.listPlaces.addElement(new oneLoc(-37.12 , 2.12 )); c.listPlaces.addElement(new oneLoc(-35.12 , 4.12 )); c.recenterMapAroundSel();

Some info about lon,lat, pixels coordinates: Internally, earthCanvas use mostly "pixels" position. In this coordinate, the world is 65536*256 pixels longs (longitude) and 65536*256 long in latitude.
To convert a pixel coordinates into a screen coordinate, you most apply a zoom factor.
screenX=x/(1<<zoom); screenY=y/(1<<zoom);

Note that upload of images is done in a separate Thread

Other features:
Locations are elements display on hte screen, usually using a pin.

KeyBehavior:

There is a default behavior when user press key. keft, right, top, down, move the map, '5' switch from sateliite to map mode, 1 zoom out, 3 zoom in, etc...
You can change this behavior in two ways: - By extending the component - By implementing the MapCanasListener, and listen to key events...

Customisations:



Field Summary
 OneLoc currentSel
          This is the currently selected location...
protected  int height
           
 int id
          User id in server base...Do not use it directly...
 java.util.Vector listPlaces
          List of places to display
 boolean m_autoPositionningEnabeld
          True is map follow the last GPS position automatically...
 boolean m_autoTrack
          if true indicate that the map is automatically centered to the last GPS position
protected static int m_curSat
           
 boolean m_displayCenterArrow
          True to display a square in the center of the screen...
 boolean m_displaySize
          true if display the scale size marker when repainting the screen.
 boolean m_drawConsumption
           
 boolean m_drawLon
          True if the latitude and longitude needs to be displayed
 int[] m_gpsSegments
          Used by GPS positionning....
 oneGPSPos m_lastLocation
          last
 java.util.Vector m_listMyPlaces
          The list of user places (MyPlaces) draw on top of the screen in green...
 boolean m_modeSat
          Current display mode.
 boolean m_modeShortkey
           
 java.lang.String m_moreInfo
           
 boolean m_pause
          true if you want to pause the record, during track recording...
 boolean m_record
          boolean that tell if we are recording (true) or not the current GPS path...
 int[] m_segments
          The segments to be displayed if any.
static int m_state
           
 boolean m_useResampledImages
          This mode enable resampled image when zooming in
This might be CPU costly and can be disabeld.
 boolean m_useUSMetrics
          true is use metric are used, false for meters (default)
protected  java.lang.String[] menus
           
protected  int offx
           
protected  int offy
           
protected  int px
           
protected  int py
           
static int SAT_ASK
           
static int SAT_GOOGLE
           
static int SAT_MSN
           
static int SAT_YAHOO
           
protected static int size
           
protected static int sizeScr
           
static int ST_MOVE_PIN
           
static int ST_NORMAL
           
static int ST_ZOOM
           
 OneLoc wayPoint
          Current waypoint (or null if no way point).
protected  int width
           
protected  int zoom
           
 
Fields inherited from class javax.microedition.lcdui.game.GameCanvas
DOWN_PRESSED, FIRE_PRESSED, GAME_A_PRESSED, GAME_B_PRESSED, GAME_C_PRESSED, GAME_D_PRESSED, LEFT_PRESSED, RIGHT_PRESSED, UP_PRESSED
 
Fields inherited from class javax.microedition.lcdui.Canvas
DOWN, FIRE, GAME_A, GAME_B, GAME_C, GAME_D, KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_POUND, KEY_STAR, LEFT, RIGHT, UP
 
Constructor Summary
MapCanvas()
          Constructor of the canvas.
 
Method Summary
 boolean changeSatMode()
          Switching between satellite/mapping mode....
protected  void checkMove()
           
 void clear()
          Remove all markers, segments, etc....
 java.lang.String convertDisToString(int inDist)
          Convert a distance into a string.
static float convLat(int fromY)
           
static float convLon(int fromX)
          Concert a pixel position into a Latitude
Usfull for on-screen operation...
 void displayTrack(Track track, boolean doRecenter)
          Display a track on the map.
 void drawArrow(javax.microedition.lcdui.Graphics g, oneGPSPos pos, int x, int y, int col)
           
 void drawBottomIndicators(javax.microedition.lcdui.Graphics g, int posy)
           
 void drawDirectiontoWayPoint(javax.microedition.lcdui.Graphics g)
          Draw the arrow who represent the direction to the current waypoint from the current visible area
protected  void drawInfo(javax.microedition.lcdui.Graphics g, java.lang.String s, int px, int py, int col, int bcol)
           
 void drawSegment(javax.microedition.lcdui.Graphics g, int[] segment, int lpx, int lpy, int div, int color)
          Draw a segement on screen...Usually you don't need to use this directly ase ou should set up the variable m_segment, but could be usefull for some experiementations

 void drawString(javax.microedition.lcdui.Graphics g, java.lang.String toDisp, int x, int y)
          Utility function to display a shadowed string....
 float getAlt()
           
 int getBufferSize()
           
static java.lang.String getCoordAsString(boolean isLon, float theVal, boolean decimalMode)
          Create a string from a coordinate value (lon/lat) in decimal or degree minut form...
 double getCourseToTarget()
          Return the course from the current position to the current waypoint, -1 if no waypoint is currently selected...
 int getCurSat()
          Return the current mapping provider used (google,msn,ask)
 float getDir()
           
 int getDist()
          Give the distance from the current location to the current waypoint...
 int getDist(boolean doConvertInMeters)
           
 float getLat()
           
 int getLatPy()
          Return the current latitude (px)
 float getLon()
          Provide current Longitude, in Float
 int getLonPx()
          Return the current position, in pixels, of the center of the screen...
 boolean getModeProxy()
           
 OneLoc getSelectedItem(javax.microedition.lcdui.Graphics g, int inpx, int inpy, int zoom, int dist, boolean onlyRepaint)
           
 int getSize(int inOff)
          Return the size (in meter) of an element defined in pixel in the current view
 float getSpeed()
           
 int getZoom()
          Provide current Zoom Factor
0 is the maximum, 17 is the minimum...
 void goTo(OneLoc place)
          A variant of previous method, with a location.
 void goTo(java.lang.String inPlace)
          Goto a specific location
 void gotoLonLat(float lon, float lat, int zoom, boolean isSatellite)
           
 void gotoLonLat(java.lang.Float lon, java.lang.Float lat, int zoom, boolean isSatellite)
          Move the map to a specific location
 void iconDownloaded(OneLoc theLoc)
           
 void imageLoaded(BufferImage ima)
           
 void init()
          Initialise the map canvas thread Must be called at least one to enable the loading of the picture.
 boolean isTrackDisplayed(Track track)
          Return true if the track given as a parameter is currently displayed on screen
 void keyPressed(int keyCode)
           
 void keyPressedImpl(int keyCode)
          Provide default behavior of the canvas:
up,down,right,left move the canvas fire switch from map to zoom if no location is selected fire generate an event to the external listener if a location is selected GAME_A and GAME_B increase/decrease zoom factor GAME_C and GAME_D go to next marker
 void keyRepeated(int keyCode)
           
 java.lang.String locateURL(java.lang.String theUrl)
          Transform an URL in a "located" URL, by checking the following carcharcter, and replacing then with the specific value: !
 void paint(javax.microedition.lcdui.Graphics g)
          * * Repaint the screen....The repaint process use the buffer thread
Note that various adaptation to screen size are made....
 void paintGPSPos(javax.microedition.lcdui.Graphics g, oneGPSPos pos, int inpx, int inpy, int w, int h, int zoom)
           
 void paintIcons(javax.microedition.lcdui.Graphics g)
           
protected  void paintMenu(javax.microedition.lcdui.Graphics g, java.lang.String[] menu)
           
protected  void pointerDragged(int x, int y)
           
protected  void pointerPressed(int x, int y)
           
 void recenterMapAroundSel()
          Recenter and rescale the map, in a way that all places are visibles...Usefull to set initial setting
 void recenterMapAroundSel(int percentage)
          Equivalent to recenterMapAroundSel but with a user defined percentage of visibility
For instanve, 80 means that there is 20 percent of margin of visibility.
 void removeTrack(Track track)
          Remove the track given as a paramter from the list of currently displayed track
 void run()
           
 void saveRecords()
           
 boolean selNext(int dir)
           
 void setBufferSize(int inSize)
           
 void setClipRect(int inMinx, int inMiny, int inWidth, int inHeight)
           
 void setCurSat(int inNumSat)
           
 void setInfoOnScreen(java.lang.String inInfo)
           
 void setListener(MapCanvasListener inListener)
          Set the listener to be called when the fire is pressed on a selected item...
 void setLoc(oneGPSPos newLoc)
           
 void setMode(int mode)
           
 void setModeProxy(boolean useProxy)
           
 void setOverlay(MapCustomOverlay inOverlay)
           
protected  void setSelectedLoc(OneLoc loc)
           
 void setTileSize(int inSize)
          Set the size of the tiles.
 void setZoom(int inZoom)
          Change the zoom factor...
 void sizeChanged(int w, int h)
           
 void stop()
           
 void stopZooming()
           
static int xFromLon(float inLon)
          Convert a Float in longitude to the full pixel number in "google map" coordinate, at the maximum zoom factor level (to get the equivalent at any zoom level, divide by (1<
static int yFromLat(float inLat)
          Same for latitude
 
Methods inherited from class javax.microedition.lcdui.game.GameCanvas
flushGraphics, flushGraphics, getGraphics, getKeyStates
 
Methods inherited from class javax.microedition.lcdui.Canvas
getGameAction, getKeyCode, getKeyName, hasPointerEvents, hasPointerMotionEvents, hasRepeatEvents, hideNotify, isDoubleBuffered, keyReleased, pointerReleased, repaint, repaint, serviceRepaints, setFullScreenMode, showNotify
 
Methods inherited from class javax.microedition.lcdui.Displayable
addCommand, getHeight, getTicker, getTitle, getWidth, isShown, removeCommand, setCommandListener, setTicker, setTitle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

offx

protected int offx

offy

protected int offy

width

protected int width

height

protected int height

m_segments

public int[] m_segments
The segments to be displayed if any.
This is a pair of lat/lon IN pixels...
So if segment is 3 lines longs, their are 6 elements in the array.
lat=segment[2*i]
lon=segment[2*i+1]


m_gpsSegments

public int[] m_gpsSegments
Used by GPS positionning....


currentSel

public OneLoc currentSel
This is the currently selected location... could be used externally....


listPlaces

public java.util.Vector listPlaces
List of places to display


m_listMyPlaces

public java.util.Vector m_listMyPlaces
The list of user places (MyPlaces) draw on top of the screen in green...


px

protected int px

py

protected int py

size

protected static final int size
See Also:
Constant Field Values

sizeScr

protected static int sizeScr

zoom

protected int zoom

m_modeSat

public boolean m_modeSat
Current display mode. True if in sattelite mode, false in map mode


m_drawLon

public boolean m_drawLon
True if the latitude and longitude needs to be displayed


m_displayCenterArrow

public boolean m_displayCenterArrow
True to display a square in the center of the screen...


m_displaySize

public boolean m_displaySize
true if display the scale size marker when repainting the screen.


m_useUSMetrics

public boolean m_useUSMetrics
true is use metric are used, false for meters (default)


SAT_GOOGLE

public static final int SAT_GOOGLE
See Also:
Constant Field Values

SAT_MSN

public static final int SAT_MSN
See Also:
Constant Field Values

SAT_ASK

public static final int SAT_ASK
See Also:
Constant Field Values

SAT_YAHOO

public static final int SAT_YAHOO
See Also:
Constant Field Values

m_curSat

protected static int m_curSat

m_lastLocation

public oneGPSPos m_lastLocation
last


m_autoPositionningEnabeld

public boolean m_autoPositionningEnabeld
True is map follow the last GPS position automatically...


m_useResampledImages

public boolean m_useResampledImages
This mode enable resampled image when zooming in
This might be CPU costly and can be disabeld.


wayPoint

public OneLoc wayPoint
Current waypoint (or null if no way point). By setting a waypoint, you display the "arrow" to this waypoint. Note that this variable is also set up by the user when using navigation keys if there are some element displayed.


m_modeShortkey

public boolean m_modeShortkey

m_autoTrack

public boolean m_autoTrack
if true indicate that the map is automatically centered to the last GPS position


m_record

public boolean m_record
boolean that tell if we are recording (true) or not the current GPS path...


m_pause

public boolean m_pause
true if you want to pause the record, during track recording...


ST_NORMAL

public static final int ST_NORMAL
See Also:
Constant Field Values

ST_ZOOM

public static final int ST_ZOOM
See Also:
Constant Field Values

ST_MOVE_PIN

public static final int ST_MOVE_PIN
See Also:
Constant Field Values

m_state

public static int m_state

menus

protected java.lang.String[] menus

m_moreInfo

public java.lang.String m_moreInfo

m_drawConsumption

public boolean m_drawConsumption

id

public int id
User id in server base...Do not use it directly...

Constructor Detail

MapCanvas

public MapCanvas()
Constructor of the canvas. The canvas is then put into full screen mode. Note that init() must be call before using the map.

Method Detail

init

public void init()
Initialise the map canvas thread Must be called at least one to enable the loading of the picture.


getLonPx

public int getLonPx()
Return the current position, in pixels, of the center of the screen...

Returns:
the current position (of the center of the screen) in pixel at the maximum resolution

getLatPy

public int getLatPy()
Return the current latitude (px)

Returns:
See Also:
getLonPx

setClipRect

public void setClipRect(int inMinx,
                        int inMiny,
                        int inWidth,
                        int inHeight)

getZoom

public int getZoom()
Provide current Zoom Factor
0 is the maximum, 17 is the minimum...

Returns:
zoom factor

setZoom

public void setZoom(int inZoom)
Change the zoom factor...

Note: if there was some pending image download, these are cancelled as they are probably not anymore needed...

Parameters:
inZoom - : the new zoom factor, between 0 and 17 included.

getBufferSize

public int getBufferSize()

setTileSize

public void setTileSize(int inSize)
Set the size of the tiles. Changing this will move into the "proxyMode" if it was not yet the case. If you want to go back to the normal mode (256 px tiles), use setProxyMode(false);


setModeProxy

public void setModeProxy(boolean useProxy)

getModeProxy

public boolean getModeProxy()

setBufferSize

public void setBufferSize(int inSize)

setOverlay

public void setOverlay(MapCustomOverlay inOverlay)

run

public void run()
Specified by:
run in interface java.lang.Runnable

stop

public void stop()

clear

public void clear()
Remove all markers, segments, etc....


getSelectedItem

public OneLoc getSelectedItem(javax.microedition.lcdui.Graphics g,
                              int inpx,
                              int inpy,
                              int zoom,
                              int dist,
                              boolean onlyRepaint)

sizeChanged

public void sizeChanged(int w,
                        int h)
Overrides:
sizeChanged in class javax.microedition.lcdui.Canvas

paint

public void paint(javax.microedition.lcdui.Graphics g)
* * Repaint the screen....The repaint process use the buffer thread
Note that various adaptation to screen size are made....

Overrides:
paint in class javax.microedition.lcdui.game.GameCanvas

setMode

public void setMode(int mode)

drawBottomIndicators

public void drawBottomIndicators(javax.microedition.lcdui.Graphics g,
                                 int posy)

getSize

public int getSize(int inOff)
Return the size (in meter) of an element defined in pixel in the current view

Parameters:
inPixelWidth -
Returns:
the size!

drawSegment

public void drawSegment(javax.microedition.lcdui.Graphics g,
                        int[] segment,
                        int lpx,
                        int lpy,
                        int div,
                        int color)
Draw a segement on screen...Usually you don't need to use this directly ase ou should set up the variable m_segment, but could be usefull for some experiementations

Parameters:
g -
segment -
lpx -
lpy -
div -
color -

drawInfo

protected void drawInfo(javax.microedition.lcdui.Graphics g,
                        java.lang.String s,
                        int px,
                        int py,
                        int col,
                        int bcol)

getCoordAsString

public static java.lang.String getCoordAsString(boolean isLon,
                                                float theVal,
                                                boolean decimalMode)
Create a string from a coordinate value (lon/lat) in decimal or degree minut form...

Parameters:
isLon -
theVal -
decimalMode -
Returns:

getDist

public int getDist()
Give the distance from the current location to the current waypoint...
return -1 if there is no waypoint selected...
if location is not used, then it's the distance to the current visible position.

Returns:
distance in pixels at the maximum level of zoom

getDist

public int getDist(boolean doConvertInMeters)

getCourseToTarget

public double getCourseToTarget()
Return the course from the current position to the current waypoint, -1 if no waypoint is currently selected...

Returns:
angle in radian

convertDisToString

public java.lang.String convertDisToString(int inDist)
Convert a distance into a string.
Distance is in meters! Resulting string will be in meters or kilometers...

Parameters:
inDist -
Returns:

drawDirectiontoWayPoint

public void drawDirectiontoWayPoint(javax.microedition.lcdui.Graphics g)
Draw the arrow who represent the direction to the current waypoint from the current visible area

Parameters:
g -

paintMenu

protected void paintMenu(javax.microedition.lcdui.Graphics g,
                         java.lang.String[] menu)

recenterMapAroundSel

public void recenterMapAroundSel()
Recenter and rescale the map, in a way that all places are visibles...Usefull to set initial setting


recenterMapAroundSel

public void recenterMapAroundSel(int percentage)
Equivalent to recenterMapAroundSel but with a user defined percentage of visibility
For instanve, 80 means that there is 20 percent of margin of visibility.
More than 100 means that some elements might not be visible...

Parameters:
percentage - Define margins (100=no margin, 80 means 20% of margin)

drawString

public void drawString(javax.microedition.lcdui.Graphics g,
                       java.lang.String toDisp,
                       int x,
                       int y)
Utility function to display a shadowed string....

Parameters:
g - Graphics display
toDisp - String to be displayed
x - Location on screen
y -

goTo

public void goTo(java.lang.String inPlace)
Goto a specific location

Parameters:
inPlace - contains the location in the form longitute latitude zoom_factor satmode Example:
  goTo("-122.2 37.1 10 true");
  
will display the satellite map of -122.2,37.1 with a zoom factor of 10 and in statellite mode (false for street mode)

goTo

public void goTo(OneLoc place)
A variant of previous method, with a location.
This location does not to be part of the current visible location
(for example, could be used with saved location)

Parameters:
place -

xFromLon

public static int xFromLon(float inLon)
Convert a Float in longitude to the full pixel number in "google map" coordinate, at the maximum zoom factor level (to get the equivalent at any zoom level, divide by (1<
Parameters:
inLon - the longitude in Float
Returns:

yFromLat

public static int yFromLat(float inLat)
Same for latitude

Parameters:
inLat -
Returns:

gotoLonLat

public void gotoLonLat(java.lang.Float lon,
                       java.lang.Float lat,
                       int zoom,
                       boolean isSatellite)
Move the map to a specific location

Parameters:
lon -
lat -
zoom - 0 is maximum factor, 16 is minimum
isSatellite - true if display satellite maps, false for stree map

gotoLonLat

public void gotoLonLat(float lon,
                       float lat,
                       int zoom,
                       boolean isSatellite)

getLon

public float getLon()
Provide current Longitude, in Float

Returns:

convLon

public static float convLon(int fromX)
Concert a pixel position into a Latitude
Usfull for on-screen operation...
Note: This static function can be called by any other classe, the only link variable in mapCanvas is the satellite mode.

Parameters:
fromX - pixel position (maximum zoom factor)
Returns:
computed longitude from this pixel position

getLat

public float getLat()
Returns:
current Latitude in Float

getSpeed

public float getSpeed()
Returns:
current speed in Float

getDir

public float getDir()
Returns:
current dir

getAlt

public float getAlt()
Returns:
current speed in Float

convLat

public static float convLat(int fromY)
Parameters:
fromY - pixel
Returns:
computed lagitude from this pixel position

selNext

public boolean selNext(int dir)

setInfoOnScreen

public void setInfoOnScreen(java.lang.String inInfo)

paintIcons

public void paintIcons(javax.microedition.lcdui.Graphics g)

pointerPressed

protected void pointerPressed(int x,
                              int y)
Overrides:
pointerPressed in class javax.microedition.lcdui.Canvas

checkMove

protected void checkMove()

setSelectedLoc

protected void setSelectedLoc(OneLoc loc)

pointerDragged

protected void pointerDragged(int x,
                              int y)
Overrides:
pointerDragged in class javax.microedition.lcdui.Canvas

keyRepeated

public void keyRepeated(int keyCode)
Overrides:
keyRepeated in class javax.microedition.lcdui.Canvas

keyPressed

public void keyPressed(int keyCode)
Overrides:
keyPressed in class javax.microedition.lcdui.Canvas

keyPressedImpl

public void keyPressedImpl(int keyCode)
Provide default behavior of the canvas:

See Also:
Canvas.keyPressed(int)

stopZooming

public void stopZooming()

changeSatMode

public boolean changeSatMode()
Switching between satellite/mapping mode.... return true is we are now in satellite, false in mapping mode


getCurSat

public int getCurSat()
Return the current mapping provider used (google,msn,ask)

Returns:
0,1 or 2, see constant defined

setCurSat

public void setCurSat(int inNumSat)

setListener

public void setListener(MapCanvasListener inListener)
Set the listener to be called when the fire is pressed on a selected item...

Parameters:
inListener -

iconDownloaded

public void iconDownloaded(OneLoc theLoc)
Specified by:
iconDownloaded in interface OneLocListener

drawArrow

public void drawArrow(javax.microedition.lcdui.Graphics g,
                      oneGPSPos pos,
                      int x,
                      int y,
                      int col)

paintGPSPos

public void paintGPSPos(javax.microedition.lcdui.Graphics g,
                        oneGPSPos pos,
                        int inpx,
                        int inpy,
                        int w,
                        int h,
                        int zoom)

setLoc

public void setLoc(oneGPSPos newLoc)

saveRecords

public void saveRecords()

imageLoaded

public void imageLoaded(BufferImage ima)
Specified by:
imageLoaded in interface BufferImageListener

displayTrack

public void displayTrack(Track track,
                         boolean doRecenter)
Display a track on the map.

Parameters:
track: - th track to be displayed
doRecenter - true if we need to rtecetner the view around the content of the track

isTrackDisplayed

public boolean isTrackDisplayed(Track track)
Return true if the track given as a parameter is currently displayed on screen


removeTrack

public void removeTrack(Track track)
Remove the track given as a paramter from the list of currently displayed track


locateURL

public java.lang.String locateURL(java.lang.String theUrl)
Transform an URL in a "located" URL, by checking the following carcharcter, and replacing then with the specific value: !minlat! !minlon! !maxlat! !maxlon! !lat! !lon!