The biggest struggle I've had with mapping in PCDB Waypoints is in drawing the grid lines.  The literature here is very skimpy, and in particular it's not easy to work out what pixel co-ordinates to use to draw something on the portion of the map that is currently on display.  But with the help of a few useful posts on Stack Overflow, I've managed to put together a class called  GridOverlay which extends Overlay and works with maps supported by OSMDroid.  OSMDroid is an alternative to the mapping facilities provided by Google Maps, which are limited to use with only maps provided by Google.  (Goodness knows what Google hope to achieve by that limitation.)  OSMDroid facilities aOverlays are usually used to draw markers of various sorts on the Map.  GridOverlay doesn't store any markers, or Points (as PathOverlay does), and doesn't have any onTap methods.  Instead, it contains a few getters and setters, and a draw() method which does all the work.

You can find the java code of GridOverlay here.

The draw() method calculates the grid size, aiming to get a minimum of two latitude lines within the MapView's current display area.  It draws the grid only on the portion of the map on display, and redraws it each time it is called, so applications using this overlay should call their MapView's invalidate() method after every zoom or scroll action.  It puts the grid latitude or longitude on each grid line, with longitudes displayed vertically.  The format in which these are displayed can be varied.  Supported formats are LLD (degrees and fractions, e.g. 52.03365°), LLDM (degrees, minutes and fractions of minutes, e.g. 004°13.255'W) or LLDMS (degrees, minutes, seconds and fractions of seconds, e.g. 52°13' 4.3").

To include grid lines on your MapView (represented here as mMapView), define a GridOverlay and add it the map's overlays, thus:
public void  drawGridLines()
{
        GridOverlay ol = new GridOverlay(
                     Color.GRAY
                      GridOverlay.LLDM,
                      mResourceProxy);

       ol.setStrokeWidth(1.0f);
        mMapView.getOverlays().add(ol);
}

Define a MapListener handler for the MapView, thus:



mMapView.setMapListener(new MapListener()
 {
         @Override
         public boolean  onZoom(ZoomEvent ev) 
          {
                updateMapPosition();
                mMapView.invalidate();

                return false;
         }

        @Override
        public boolean onScroll(ScrollEvent ev) 
        {
                updateMapPosition();
                mMapView.invalidate();

                return false;
        }
 });

(In my implementation, updateMapPosition() records the map's centre point and co-ordinates in the text boxes to the right hand side, as shown in the screen shots in my previous blog.)
 





Leave a Reply.