I'm having a problem with my javascript code.
It basically consists of a script that accesses the exif of a photo and then shows it on an HTML page, more specifically the latitude and longitude of it.
The idea is to then use both the latitude and longitude on a Google maps iframe to then show the location that photo was taken. But that's later as this is only a test page I'm using to ecperiment the workings of it.
The thing is that before using the latitude and longitude, it has to be converted into a format recognised by that same iframe:
EX: the output of "latitude = EXIF.getTag(this, "GPSLatitude")" would be:
41,23.0692,0
and it needs to be something like this:
41.38448666666667
I have a function that does just that and it works if i do something like this:
var toDecimal = function (number) {
var d = Math.floor(number[0]);
var m = Math.floor(number[1]);
var s = ((number[1]%1)*60);
var dms = d+(m/60)+(s/3600);
return dms
};
var latitude_teste="41,23.0692,0";
var latitude_array = latitude_teste.split(',');
var latitude_final = toDecimal(latitude_array);
var local_lat_final = document.getElementById("local_lat_final");
local_lat_final.innerHTML = `${latitude_final}`;
But if I try to use this method above like this (latitude being the variable from the function getExif()):
var latitude_teste=latitude;
var latitude_array = latitude_teste.split(',');
var latitude_final = toDecimal(latitude_array);
var local_lat_final = document.getElementById("local_lat_final");
local_lat_final.innerHTML = `${latitude_final}`;
It won't show any value at all for latitude...
Why am I having this problem? Is it some syntax mistake?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>EXIF</title>
<style>
img{
width: 500px;
max-height: auto;
}
</style>
</head>
<body>
<img src="https://c1.staticflickr.com/5/4867/30883801817_bf122bc498_o.jpg" id="img1" />
<h1>Latitude Exif</h1>
<p id="local_lat"></p>
<h1>Longitude Exif</h1>
<p id="local_lon"></p>
<h1>Latitude Final</h1>
<p id="local_lat_final"></p>
<h1>Longitude Final</h1>
<p id="local_lon_final"></p>
<script src="exif.js"></script>
<script>
window.onload=getExif;
function getExif() {
var img1 = document.getElementById("img1");
EXIF.getData(img1, function() {
latitude = EXIF.getTag(this, "GPSLatitude");
var longitude = EXIF.getTag(this, "GPSLongitude");
var local_lat = document.getElementById("local_lat");
var local_lon = document.getElementById("local_lon");
local_lat.innerHTML = `${latitude}`;
local_lon.innerHTML = `${longitude}`;
});
}
/*função para converter latitude e longitude do exif.js
em graus decimais, legíveis pelo google maps*/
var toDecimal = function (number) {
var d = Math.floor(number[0]);
var m = Math.floor(number[1]);
var s = ((number[1]%1)*60);
var dms= d+(m/60)+(s/3600);
return dms
};
var latitude_teste="41,23.0692,0";
var latitude_array = latitude_teste.split(',');
var latitude_final = toDecimal(latitude_array);
var local_lat_final = document.getElementById("local_lat_final");
local_lat_final.innerHTML = `${latitude_final}`;
/*THIS PIECE OF CODE BELOW WON'T WORK if used to replace the one above*/
/*
var latitude_teste=latitude;
var latitude_array = latitude_teste.split(',');
var latitude_final = toDecimal(latitude_array);
var local_lat_final = document.getElementById("local_lat_final");
local_lat_final.innerHTML = `${latitude_final}`;
*/
var longitude_teste="2,6.4013,0";
var longitude_array = longitude_teste.split(',');
var longitude_final = toDecimal(longitude_array);
var local_lon_final = document.getElementById("local_lon_final");
local_lon_final.innerHTML = `${longitude_final}`;
</script>
</body>
</html>