%
% Setups the sub plot we'll use to draw the current position of the
% receiver.
%
% Input:
%  hf - handle to the figure
%  SetInvisibile -if 1 don't render the plot to the screen
%  PVT - truth position
%  LonMargin - how many meters of margin do we want to add (Longitude)
%  LatMargin - how many meters of margin do we want to add (Latitude)
function setupMap(hf,SetInvisible,PVT,LonMargin,LatMargin)

% POS Pac Column index
dTime = 1;
dLat  = 2;
dLon  = 3;

if(SetInvisible == 1)
  set(hf,'Visible','off');
end


subtightplot(2,2,1,[0.04,0.04]);

lat = mean(PVT(:,dLat)) * pi / 180;
lon = mean(PVT(:,dLon)) * pi / 180;

a  = 6378137.0; % Earth radius
f  = 1 / 298.257223563 ;
e2 = 2*f - f*f ;

% Earth flattening
W = sqrt( 1 - e2 * sin(lat) * sin(lat)) ;
N = a / W ;
M = a * ( 1 - e2 ) / (W * W * W) ;
NcosRefLat = N * cos(lat);

% Compute the degrees / metres at the current position
LatScale   = 180 / (M*pi);
LongScale  = 180 / (NcosRefLat*pi);

A(1) = min(PVT(:,dLon)) - LongScale*LonMargin;
A(2) = max(PVT(:,dLon)) + LongScale*LonMargin;
A(3) = min(PVT(:,dLat)) - LatScale*LatMargin;
A(4) = max(PVT(:,dLat)) + LatScale*LatMargin;
axis(A);
disp('Getting satellite map data from Google');
plot_google_map('maptype','satellite');
disp('Got Sat Map');

