package cgeo.geocaching.maps;

import cgeo.geocaching.DataStore;
import cgeo.geocaching.location.Geopoint;
import cgeo.geocaching.location.Viewport;
import cgeo.geocaching.maps.interfaces.GeneralOverlay;
import cgeo.geocaching.maps.interfaces.MapProjectionImpl;
import cgeo.geocaching.maps.interfaces.MapViewImpl;
import cgeo.geocaching.maps.interfaces.OverlayImpl;

import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;

import java.util.ArrayList;

public class PositionAndScaleOverlay implements GeneralOverlay {
    private OverlayImpl ovlImpl = null;

    PositionDrawer positionDrawer = null;
    ScaleDrawer scaleDrawer = null;
    DirectionDrawer directionDrawer = null;
    DistanceDrawer distanceDrawer = null;

    public PositionAndScaleOverlay(final OverlayImpl ovlImpl, final MapViewImpl mapView, Geopoint coords, final String geocode) {
        this.ovlImpl = ovlImpl;
        positionDrawer = new PositionDrawer();
        scaleDrawer = new ScaleDrawer();

        if (coords == null && geocode != null) {
            final Viewport bounds = DataStore.getBounds(geocode);
            if (bounds != null) {
                coords = bounds.center;
            }
        }
        if (coords != null) {
            directionDrawer = new DirectionDrawer(coords);
            distanceDrawer = new DistanceDrawer(mapView, coords);
        }
    }

    public void setCoordinates(final Location coordinatesIn) {
        positionDrawer.setCoordinates(coordinatesIn);
        if (directionDrawer != null) {
            directionDrawer.setCoordinates(coordinatesIn);
            distanceDrawer.setCoordinates(coordinatesIn);
        }

    }

    public Location getCoordinates() {
        return positionDrawer.getCoordinates();
    }

    public void setHeading(final float bearingNow) {
        positionDrawer.setHeading(bearingNow);
    }

    public float getHeading() {
        return positionDrawer.getHeading();
    }

    @Override
    public void drawOverlayBitmap(final Canvas canvas, final Point drawPosition,
            final MapProjectionImpl projection, final byte drawZoomLevel) {

        drawInternal(canvas, projection, getOverlayImpl().getMapViewImpl());
    }

    @Override
    public void draw(final Canvas canvas, final MapViewImpl mapView, final boolean shadow) {

        drawInternal(canvas, mapView.getMapProjection(), mapView);
    }

    private void drawInternal(final Canvas canvas, final MapProjectionImpl projection, final MapViewImpl mapView) {
        directionDrawer.drawDirection(canvas, projection);
        positionDrawer.drawPosition(canvas, projection);
        scaleDrawer.drawScale(canvas, mapView);
        distanceDrawer.drawDistance(canvas);
    }

    @Override
    public OverlayImpl getOverlayImpl() {
        return this.ovlImpl;
    }

    public ArrayList<Location> getHistory() {
        return positionDrawer.getHistory();
    }

    public void setHistory(final ArrayList<Location> history) {
        positionDrawer.setHistory(history);
    }
}

@@ -846,16 +846,22 @@
 mapView,
+ final
  Geopoin
@@ -1038,27 +1038,8 @@
 rds 
-== null && geocode 
 != n
@@ -1061,176 +1061,245 @@
     
-final Viewport bounds = DataStore.getBounds(geocode);%0A            if (bounds != null) %7B%0A                coords = bounds.center;%0A            %7D%0A        %7D%0A        if (coor
+directionDrawer = new DirectionDrawer(coords);%0A            distanceDrawer = new DistanceDrawer(mapView, coords);%0A        %7D else if (geocode != null) %7B%0A            final Viewport bounds = DataStore.getBounds(geocode);%0A            if (boun
 ds !
@@ -1300,32 +1300,36 @@
 unds != null) %7B%0A
+    
             dire
@@ -1358,33 +1358,44 @@
 ctionDrawer(
-coords);%0A
+bounds.center);%0A    
             
@@ -1435,32 +1435,53 @@
 er(mapView, 
-coords);
+bounds.center);%0A            %7D
 %0A        %7D%0A 
@@ -2611,24 +2611,67 @@
  mapView) %7B%0A
+        if (directionDrawer != null) %7B%0A    
         dire
@@ -2709,32 +2709,42 @@
 s, projection);%0A
+        %7D%0A
         position
@@ -2828,24 +2828,66 @@
 , mapView);%0A
+        if (distanceDrawer != null) %7B%0A    
         dist
@@ -2915,24 +2915,34 @@
 ce(canvas);%0A
+        %7D%0A
     %7D%0A%0A    @

Fix NullPointerException when a map with no destination is opened