Map disappears after clicking on button - Google maps API

The problem is that when I click on the "find, delete, update, add" buttons after selecting a coordinate from index.php the map disappears and everything works fine.

This issue started when I retrieved the lat and lng for the map marker from the database using php. It works fine when marker values are static like

var location={lat: 14.555071, lng: -90.734250};

THIS IS MY INDEX.PHP

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html lang="es">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        
         
        <title>Proyecto de Redes</title>
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        
         
        
    </head>
    <body>
        
        <br>
    <center><font size='5'>Listado de Tramas</font></center>
    
    <br><br>
        
        <?php
    $servername = "localhost";
$username = "root";
$password = "";
$dbname = "gps";

// Create connection
$conexion = new mysqli($servername, $username, $password, $dbname);


        $consulta="select * from trama";
        $resultados=  mysqli_query($conexion, $consulta);
        
        
            
            if($resultados==false)
            {
                echo "error en la consulta2";
            }
            
            echo "<div class='table-container'><table><tr id='franja'><th width=10%></th><th width=20%>Trama</th><th width=15%>Latitud</th><th width=15%>Longitud</th><th width=15%>Fecha</th><th width=15%>Geocerca</th></tr>";
            
            while(($fila=  mysqli_fetch_array($resultados, MYSQLI_ASSOC))==true)
            {
                echo "<tr><td><a href='mapa.php?cod_trama=".$fila['cod_trama']."&latitud=".$fila['latitude']."&longitud=".$fila['longitude']."'>Seleccionar</td> ";
              
                echo "<td>".$fila['trama']."</td></a> ";
                echo "<td>".$fila['latitude']."</td> ";
                echo "<td>".$fila['longitude']."</td> ";
                echo "<td>".$fila['fecha']."</td> ";
                echo "<td>".$fila['geocerca']."</td> ";

                echo "</tr>";
            }
            
            echo "</table></div>";
            echo "<br><br>";  
         
            
         
        
     
      ?>
     
    </body>
</html>

THIS IS MAPA.PHP

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <?php
     class Location {

    public $latitude;
    public $longitude;

    public function __construct($latitude, $longitude) {
        $this->latitude = $latitude;
        $this->longitude = $longitude;
    }
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gps";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Fallo conexión: " . $conn->connect_error);
} 

$sql = "SELECT latitude, longitude FROM geocerca";
$result = $conn->query($sql);

$locations = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        array_push($locations, new Location($row["latitude"], $row["longitude"]));
    }
} else {
    echo "NO HAY COORDENADAS!";
}
$conn->close();
    ?>
    <head>
        <title>GeoCercas</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>#mymap {width: 80%; height: 600px;}</style>
         <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCex4QTqx_NqpnuLgSUfdRaLPPmnVDX3es&callback=initMap"
  type="text/javascript"></script>
        
        <script>
 var locations = <?php echo json_encode($locations); ?>;

            function init(){
                var mapDiv = document.getElementById("mymap");
                var mapOptions ={
                    center: new google.maps.LatLng(14.555071, -90.734250),
                    zoom: 13,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(mapDiv, mapOptions);
        <?php
                    $latitud_recibida=$_REQUEST['latitud'];
                    $longitud_recibida=$_REQUEST['longitud']; 
                
                    echo "var uluru ={lat: $latitud_recibida, lng: $longitud_recibida};";
                ?>        
        
                var marker = new google.maps.Marker({
                    position: uluru,
                    map:map,
                    draggable:true
                });
                
//ANTIGUA  
                for (var i=0; i<2; i++){
                    for (var j=0; i<2; i++){
                var Antigua = new google.maps.Rectangle({
                    map: map,
                    bounds: new google.maps.LatLngBounds(
                            
                                new google.maps.LatLng(locations[j].latitude, locations[j].longitude),
                                new google.maps.LatLng(locations[i].latitude, locations[i].longitude)
                
                            ),
                    editable: true,
                    draggable:true
                
                })}};
                
                google.maps.event.addListener(Antigua, "bounds_changed", function(){
                   document.getElementById("info").innerHTML = Antigua  .getBounds();
                }) 
                
//CIUDAD VIEJA
            for (var i=2; i<4; i++){
                    for (var j=2; i<4; i++){
                var CV = new google.maps.Rectangle({
                    map: map,
                    bounds: new google.maps.LatLngBounds(
                            
                                new google.maps.LatLng(locations[j].latitude, locations[j].longitude),
                                new google.maps.LatLng(locations[i].latitude, locations[i].longitude)
                
                            ),
                    editable: true,
                    draggable:true
                
                })}};
                
                google.maps.event.addListener(CV, "bounds_changed", function(){
                   document.getElementById("info").innerHTML = CV.getBounds();
                }) 
//JOCOTENANGO
            for (var i=4; i<6; i++){
                    for (var j=4; i<6; i++){
                var Jocotenango = new google.maps.Rectangle({
                    map: map,
                    bounds: new google.maps.LatLngBounds(
                            
                                new google.maps.LatLng(locations[j].latitude, locations[j].longitude),
                                new google.maps.LatLng(locations[i].latitude, locations[i].longitude)
                
                            ),
                    editable: true,
                    draggable:true
                
                })}};
                
                google.maps.event.addListener(Jocotenango, "bounds_changed", function(){
                   document.getElementById("info").innerHTML = Jocotenango.getBounds();
                })
                                
            }
            window.onload = init;
        </script>
    </head>
    <body>
        <?php
      $host= "localhost";
        $user="root";
        $password="";
        $database="gps";
        
                
        $cod_geocerca="";
        $coordenada1="";
        $coodernada2="";
        $ubicacion="";
        
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        try{
            $connect = mysqli_connect($host, $user, $password, $database);
        } catch (Exception $ex) {
            echo     'error al conectar';
        }

        
        function getPosts(){
            $posts = array();
            $posts[0] = $_POST['cod_geocerca'];
            $posts[1] = $_POST['coordenada1'];
            $posts[2] = $_POST['coordenada2'];
            $posts[3] = $_POST['ubicacion'];
            return $posts;
        }
        
        if(isset($_POST['search'])){
            $data = getPosts();
            $search_Query = "SELECT * FROM geocerca WHERE cod_geocerca = $data[0]";
            $search_Result= mysqli_query($connect, $search_Query);
            
            if($search_Result){
                if(mysqli_num_rows($search_Result)){
                    while($row = mysqli_fetch_array($search_Result)){
                        $cod_geocerca=$row['cod_geocerca'];
                        $coordenada1=$row['latitude'];
                        $coodernada2=$row['longitude'];
                        $ubicacion=$row['ubicacion'];
                    }
                }else{
                    echo 'No hay coordenadas para este id';
                }
            }else{
                echo 'Error de resultado';
            }
        }
        
        if (isset($_POST['insert'])){
            $data =  getPosts();
            $insert_Query = "INSERT INTO `geocerca`(`ubicacion`, `latitude`, `longitude`) VALUES ('$data[3]',$data[1],$data[2])";
            try{
                $insert_Result = mysqli_query($connect, $insert_Query);
                if($insert_Result){
                    if(mysqli_affected_rows($connect)>0){
                        echo 'coordenadas insertadas';
                    }else{
                        echo 'datos NO insertados';
                    }
                }
            } catch (Exception $ex) {
                echo 'error al insertar'.$ex->getMessage();
            }
        }
        
        if (isset($_POST['delete'])){
            $data =  getPosts();
            $delete_Query = "DELETE FROM `geocerca` WHERE `cod_geocerca` = $data[0]";
            try{
                $delete_Result = mysqli_query($connect, $delete_Query);
                if($delete_Result){
                    if(mysqli_affected_rows($connect)>0){
                        echo 'coordenadas eliminadas';
                    }else{
                        echo 'datos NO eliminados';
                    }
                }
            } catch (Exception $ex) {
                echo 'error al eliminar'.$ex->getMessage();
            }
        }
        
        if (isset($_POST['update'])){
            $data =  getPosts();
            $update_Query = "UPDATE `geocerca` SET `ubicacion`='$data[3]',`latitude`=$data[1],`longitude`=$data[2] WHERE `cod_geocerca` = $data[0]";
            try{
                $update_Result = mysqli_query($connect, $update_Query);
                if($update_Result){
                    if(mysqli_affected_rows($connect)>0){
                        echo 'coordenadas actualizadas';
                    }else{
                        echo 'datos NO actualizadas';
                    }
                }
            } catch (Exception $ex) {
                echo 'error al actualizar'.$ex->getMessage();
            }
        }  
        ?>
        <h2>Geocercas</h2>
        <div id="mymap"> </div>
        <div id="info"> </div>
        <style>
            #coordenadas
            {
             position: absolute;
             right: 30px;
             top: 65px;
            }
        </style>
          <div id="coordenadas">
              Para una geocerca debe ingresar<br>
              4 coordenadas<br><br>
              <form action="mapa.php" method="post">
                <input type ="number" name="cod_geocerca" placeholder="cod_geocerca" value="<?php echo $cod_geocerca;?>"><br><br>
                <input type ="number" name="coordenada1" placeholder="coordenada1" value="<?php echo $coordenada1;?>"><br><br>
                <input type ="number" name="coordenada2" placeholder="coordenada2" value="<?php echo $coodernada2;?>"><br><br>
                <input type ="text" name="ubicacion" placeholder="ubicacion" value="<?php echo $ubicacion;?>"><br><br>
                <div>
                    <input type ="submit" name="insert" value="Add">
                    <input type ="submit" name="update" value="Update">
                    <input type ="submit" name="delete" value="Delete">
                    <input type ="submit" name="search" value="Find">
                    
                </div>
            </form>
        </div>
        
    </body>
</html>