So I have been searching for a way to get current location in java (not using any Android APIs) and something that is quite accurate. What I have so far is running an HTML file inside of Java and what I want to do is retrieve the GPS coordinates from a google API (html file).
What I have so far:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class HtmlRun {
public static void main(String[] args) throws IOException {
File htmlFile = new File("findLocation.html");
Desktop.getDesktop().browse(htmlFile.toURI());
}
}
<!DOCTYPE html>
<html>
<head>
<title>Geolocation</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
var infoWindow = new google.maps.InfoWindow({map: map});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBTQzs7iZUtr7v-VbvAWhAql5LiQ9zZrFE&callback=initMap">
</script>
</body>
</html>
It occurs over here:
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
But I don't know how to retrieve that data back into my Java application.
This here:
public static void main(String[] args) throws IOException {
File htmlFile = new File("findLocation.html");
Desktop.getDesktop().browse(htmlFile.toURI());
}
is just opening the html doc in a web browser, after that you have no control AT ALL about what is happening with the web content, the user can even click all the buttons/ pois in the map and you will never get access to that information.
What you need is a webserver that SERVES the content of that app, you can implement REST/full services or even WebSockets to exchange the data between Browser and server