Practica5 GenerarKML

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Para leer posiciones cada X segundos y guardar los datos en la base de datos en servidor.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML5 Geolocalización</title>
<script type="text/javascript">
function showPosition(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
var positionInfo = "Your current position is (" + "Latitude: " + position.coords.latitude
+ ", " + "Longitude: " + position.coords.longitude + ")";
var lon2 = position.coords.longitude
var lat2 = position.coords.latitude
var time2 =new Date(position.timestamp)
//document.getElementById("result").innerHTML =
positionInfo;
//document.getElementById("lon").innerHTML = lon2;
//document.getElementById("lat").innerHTML = lat2;
//document.getElementById("tim").innerHTML = time2;
window.location.href = 'insert_dato_in_direct.php?lon_in=' +
lon2 + '&lat_in=' + lat2;
//setTimeout ("window.location.href =
'insert_dato_in_direct.php?lon_in=' + lon2 + '&lat_in=' + lat2;", 4000);
//window.open('insert_dato_in_direct.php?lon_in=' + lon2 +
'&lat_in=' + lat2;, '_blank');
});
} else{
alert("Sorry, your browser does not support HTML5 geolocation.");
}
}
</script>
</head>
<body>
<p id="lon">Esperando datos de posicionamiento...</p>
<!--
<p id="lat">Esperando Latitud</p>
<p id="tim">Esperando fecha y hora</p>
-->
<div id="number"></div>
<script>
var n = 0;
var l = document.getElementById("number");
window.setInterval(function(){
l.innerHTML = n;
n++;
},1000);
setTimeout ("showPosition();", 10000);
//showPosition();
</script>
<!--
<div id="result">
</div>
<button type="button" onclick="showPosition();">Show Position</button>
-->
</body>
</html>

<?php
$servername = "localhost:3306";
$database = "GISWEB";
$username = "GISWEB";
$password = "Gisweb2018";
$lat_in1=$_GET['lat_in'];
$lon_in1=$_GET['lon_in'];
echo "Longitud= $lon_in1<br>";
echo "Latitud= $lat_in1<br>";
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// echo "Conectado correctamente<br>";
$sql = "INSERT INTO CCF (longitud, latitud)
VALUES ($lon_in1, $lat_in1)";

// use exec() because no results are returned


$conn->exec($sql);
echo "Guadados datos correctamente<br>";
}
catch(PDOException $e)
{
echo "Fallo: " . $e->getMessage();
}
?>
<script>
//setTimeout ("window.location.href = 'get_location_insert.html';", 4000);
setTimeout ("window.history.back();", 4000);

//window.location.href = 'get_location_insert.html';
//<a href="javascript:window.history.back()">Volver a la página anterior</a>
</script>

JS – DOM:

El DOM es un estándar de W3C (World Wide Web Consortium).

El DOM define un estándar para acceder a los documentos:

"El W3C Document Object Model (DOM) es una plataforma y una interfaz de lenguaje neutral
que permite a los programas y scripts acceder y actualizar dinámicamente el contenido, la
estructura y el estilo de un documento".
El estándar W3C DOM está separado en 3 partes diferentes:

Core DOM - modelo estándar para todos los tipos de documentos


XML DOM - modelo estándar para documentos XML
HTML DOM - modelo estándar para documentos HTML

PRACTICA 5: Crear un fichero PHP que nos permita leer los datos de posiciones guardados en
la base de datos y generar un fichero KML que muestre el recorrido utilizando DOM

Ejemplo:
<?php

Se conecta con la base de datos y abre los registros con el SELECT


Si va todo bien genera el fichero:

// Creates the Document.


$dom = new DOMDocument('1.0', 'UTF-8');

// Creates the root KML element and appends it to the root document.
$node = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml');
$parNode = $dom->appendChild($node);

// Creates a KML Document element and append it to the KML element.


$dnode = $dom->createElement('Document');
$docNode = $parNode->appendChild($dnode);

// Creates the two Style elements, one for restaurant and one for bar, and append the
elements to the Document element.
$restStyleNode = $dom->createElement('Style');
$restStyleNode->setAttribute('id', 'restaurantStyle');
$restIconstyleNode = $dom->createElement('IconStyle');
$restIconstyleNode->setAttribute('id', 'restaurantIcon');
$restIconNode = $dom->createElement('Icon');
$restHref = $dom->createElement('href',
'http://maps.google.com/mapfiles/kml/pal2/icon63.png');
$restIconNode->appendChild($restHref);
$restIconstyleNode->appendChild($restIconNode);
$restStyleNode->appendChild($restIconstyleNode);
$docNode->appendChild($restStyleNode);

$barStyleNode = $dom->createElement('Style');
$barStyleNode->setAttribute('id', 'barStyle');
$barIconstyleNode = $dom->createElement('IconStyle');
$barIconstyleNode->setAttribute('id', 'barIcon');
$barIconNode = $dom->createElement('Icon');
$barHref = $dom->createElement('href',
'http://maps.google.com/mapfiles/kml/pal2/icon27.png');
$barIconNode->appendChild($barHref);
$barIconstyleNode->appendChild($barIconNode);
$barStyleNode->appendChild($barIconstyleNode);
$docNode->appendChild($barStyleNode);
// Iterates through the MySQL results, creating one Placemark for each row.
while ($row = @mysql_fetch_assoc($result)) Sería mientras que existen registros
{
// Creates a Placemark and append it to the Document.

$node = $dom->createElement('Placemark');
$placeNode = $docNode->appendChild($node);

// Creates an id attribute and assign it the value of id column.


$placeNode->setAttribute('id', 'placemark' . $row['id']);

// Create name, and description elements and assigns them the values of the name and
address columns from the results.
$nameNode = $dom->createElement('name',htmlentities($row['name']));
$placeNode->appendChild($nameNode);
$descNode = $dom->createElement('description', $row['address']);
$placeNode->appendChild($descNode);
$styleUrl = $dom->createElement('styleUrl', '#' . $row['type'] . 'Style');
$placeNode->appendChild($styleUrl);

// Creates a Point element.


$pointNode = $dom->createElement('Point');
$placeNode->appendChild($pointNode);

// Creates a coordinates element and gives it the value of the lng and lat columns from the
results.
$coorStr = $row['lng'] . ',' . $row['lat'];
$coorNode = $dom->createElement('coordinates', $coorStr);
$pointNode->appendChild($coorNode);
}

$kmlOutput = $dom->saveXML();
header('Content-type: application/vnd.google-earth.kml+xml');
echo $kmlOutput;
file_put_contents($nombre_fichero, $kmlOutput);
?>

You might also like