Кажется достаточно простым, я также добавил дату.
let myapp = {
x: document.getElementById("getGeo"),
lat: document.getElementById("lat"),
lng: document.getElementById("lng"),
dateU: document.getElementById("updated")
};
function getLocation() {
myapp.x.innerHTML = 'Go forit';
let d = new Date();
let ds = d.toLocaleDateString('en-CA');
// console.log(ds);
myapp.dateU.value =ds;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
"Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
myapp.x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
myapp.lat.value = position.coords.latitude;
myapp.lng.value = position.coords.longitude;
}
function showError(error) {
myapp.lat.value = "";
myapp.lng.value = "";
myapp.x.innerHTML = "Woops: " + error.code +
"<br>What: " + error.message;
}
<button onclick="getLocation()">Get Geo Location</button>
<p id="getGeo">xxx</p>
<form action="insert.php" method="post">
<label id="id">Location name:</label><br/>
<input type="text" name="id"><br/>
<label >Latitude:</label><br/>
<input id="lat" type="text" name="lat" value="WHAT HERE??"><br/>
<label >Longitude:</label><br/>
<input id="lng" type="text" name="lng" value="WHAT HERE??"><br/>
<label >Updated:</label><br/>
<input id="updated" type="date" name="updated"><br/>
<br>
<button type="submit" name="save">Insert new record</button>
</form>