Private Sub GE_Update_Chart()
'************************************************************************************

'This procedure updates the GE chart overlay

'The input parameters are:

'       None

'Notes on this procedure:

'       o For a procedure in greater C_wiz to update the GE chart overlay it:
'           o Sets the GE_status_chart_update flag to 1
'           o Waits for GE_interrogate_Timer to call this procedure
'       o See the notes in GE_interrogate_Timer for the reasons why

'************************************************************************************
    
    'Flag GE chart update in progress
    GE_status_chart_update = 2

'************************************************************************************

'Set up chart parameters...

'************************************************************************************
       
    'Some C_wiz co-existence code here, the objective being to establish...
    
    '... chart kml file name
    kml$ = ...
    
    '... chart top LH corner latitude and longitude
    chart_lat1# = ...
    chart_long1# = ...
    
    '... chart bottom RH corner latitude and longitude
    chart_lat2# = ...
    chart_long2# = ...
        
'************************************************************************************

'Flush the current chart overlay...

'************************************************************************************

    'If a chart currently overlaid on GE...
    If GE_last_chart <> "" Then
    
        'Open minimal chart kml file for output
        File_xkm = FreeFile
        Open mydirectory + GE_last_chart + ".kml" For Output As #File_xkm
        
        'Build minimal kml file content
        Print #File_xkm, GE_kml_header1
        Print #File_xkm, GE_kml_header2
        Print #File_xkm, "</kml>"
            
        'Close minimal chart kml file
        Close #File_xkm
        
        'Send minimal chart kml file to GE (this will flush the chart overlay from
        'GE) and if successful flush name of last chart overlaid on GE
        If GE_Send_Kml(GE_last_chart) Then GE_last_chart = ""

    End If

'************************************************************************************

'Build chart kml file...

'************************************************************************************

    'Open chart kml file for output
    File_xkm = FreeFile
    Open mydirectory + kml$ + ".kml" For Output As #File_xkm

    'Build kml file header
    Print #File_xkm, GE_kml_header1
    Print #File_xkm, GE_kml_header2
    
    'Start description of overlay
    Print #File_xkm, "  <GroundOverlay>"
    Print #File_xkm, "    <name>" + kml$ + "</name>"
    Print #File_xkm, "    <visibility>1</visibility>"
    
    'If chart background exists...
    If kml$ <> "(none)" Then

        'If it really exists...
        If Dir(mydirectory + kml$ + ".img") <> "" Then
    
            'Set up description of overlay opacity
            Print #File_xkm, "    <color>" + Right$("0" + Hex(GE_launch_opacity), 2) + "FFFFFF</color>"
            
            'Set up description of overlay background image
            Print #File_xkm, "    <Icon>"
            Print #File_xkm, "      <href>" + mydirectory + kml$ + ".img</href>"
            Print #File_xkm, "    </Icon>"
        
            'Set up description of overlay extent - GE needs this to know where to
            'stick the overlay
            Print #File_xkm, "    <LatLonBox>"
            Print #File_xkm, "      <north>" + Format$(chart_lat1#) + "</north>"
            Print #File_xkm, "      <south>" + Format$(chart_lat2#) + "</south>"
            Print #File_xkm, "      <east>" + Format$(chart_long2#) + "</east>"
            Print #File_xkm, "      <west>" + Format$(chart_long1#) + "</west>"
            Print #File_xkm, "    </LatLonBox>"
        
        End If
    
    End If

'************************************************************************************

'Set up LookAt latitude and longitude...

'************************************************************************************
       
    'If vessel locked to cursor and has been found...
    If ... Then

        'Some C_wiz co-existence code here, establishing...
        
        '... latitude of vessel locked to cursor 
        vessel_lat# = ...
    
        '... longitude of vessel locked to cursor 
        vessel_long# = ...
        
        'Set LookAt latitude and longitude to vessel locked to cursor
        lookat_lat# = vessel_lat#
        lookat_long# = vessel_long#
    
    'Otherwise...
    Else
        
        'Set LookAt latitude and longitude to centre of overlay
        lookat_lat# = (chart_lat1# + chart_lat2#) / 2
        lookat_long# = (chart_long1# + chart_long2#) / 2
    
    End If

'************************************************************************************
'Note Note Note
'Set up description of overlay LookAt - you would think this would be unneccessary
'by virtue of the SetCamera call below, however if this is left out GE will
'(after the SetCamera call is digested) zoom in/out to frame the overlay
'************************************************************************************
    
    Print #File_xkm, "    <LookAt>"
    Print #File_xkm, "      <longitude>" + Format$(lookat_long#) + "</longitude>"
    Print #File_xkm, "      <latitude>" + Format$(lookat_lat#) + "</latitude>"
    Print #File_xkm, "      <range>" + Format$(GE_range) + "</range>"
    Print #File_xkm, "      <heading>" + Format$(GE_bearing) + "</heading>"
    Print #File_xkm, "      <tilt>" + Format$(GE_tilt) + "</tilt>"
    Print #File_xkm, "    </LookAt>"
        
    'End description of overlay
    Print #File_xkm, "  </GroundOverlay>"
    Print #File_xkm, "</kml>"
        
    'Close chart kml file
    Close #File_xkm
            
    'Send chart kml file to GE (this will add the chart overlay to GE) and if
    'successful update name of last chart overlaid on GE
    If GE_Send_Kml(kml$) Then GE_last_chart = kml$

'************************************************************************************

'Move GE view if appropriate...

'************************************************************************************
   
    'If view needs to be moved...
    If lookat_lat# <> GE_view_last_lat Or lookat_long# <> GE_view_last_long Then
        
        'Set up error handler
        On Error GoTo BAD_GE
    
        'Move the view to coincide with LookAt latitude and longitude - this call is
        'made to force the view to change at top speed ("teleport" in GE parlance -
        'the 6 parameter) thus overriding any user speed settings - this avoids the
        'poncey bounce that GE can indulge in when changing views which stuffs up
        'the range/bearing/tilt monitoring done by GE_interrogate
        GE_interface_view.FocusPointLatitude = lookat_lat#
        GE_interface_view.FocusPointLongitude = lookat_long#
        GE_interface_view.range = GE_range
        GE_interface_view.Tilt = GE_tilt
        GE_interface_view.Azimuth = GE_bearing
        Call GE_interface.SetCamera(GE_interface_view, 6)
     
        'Give GE a chance to digest it
        DoEvents
       
        'Turn default error handling back on
        On Error GoTo 0
    
        'Update latitude and longitude to which view was last moved
        GE_view_last_lat = lookat_lat#
        GE_view_last_long = lookat_long#
    
    End If
    
    'If GE chart update in progress then flag GE chart update not required (it is
    'possible for this flag to be flicked to "GE chart update required" during the
    'execution of this procedure, in which case don't change it)
    If GE_status_chart_update = 2 Then GE_status_chart_update = 0

    'Quit
    Exit Sub
    
'************************************************************************************

'This GOSUB handles a GE error...

'************************************************************************************

BAD_GE:
        
    'Turn default error handling back on
    On Error GoTo 0

    'In the next split second a similar error should occur in GE_interrogate_Timer
    'which can tidy up

End Sub