从已启动的 HTML 文件中将数据检索到 Java 应用程序
Retrieve data into Java Application from an HTML file that was launched
所以我一直在寻找一种方法来获取 java 中的当前位置(不使用任何 Android APIs)并且非常准确。到目前为止,我所拥有的是 运行 在 Java 中创建一个 HTML 文件,我想要做的是从 google API 中检索 GPS 坐标( html 个文件)。
我目前有:
HtmlRun.java
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());
}
}
findLocation.html
<!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>
当我 运行 这样做时,它会为 html 文件打开我的默认应用程序并且所有内容都已排序...但是我如何检索我需要的信息? (GPS坐标)
发生在这里:
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
但我不知道如何将该数据检索回我的 Java 应用程序。
这里是:
public static void main(String[] args) throws IOException {
File htmlFile = new File("findLocation.html");
Desktop.getDesktop().browse(htmlFile.toURI());
}
只是在网络浏览器中打开 html 文档,之后您将完全无法控制网络内容发生的情况,用户甚至可以单击地图中的所有按钮/位置而且您将永远无法访问该信息。
您需要的是一个为该应用程序的内容提供服务的网络服务器,您可以实施 REST/full 服务甚至 WebSockets 以在浏览器和服务器之间交换数据
所以我一直在寻找一种方法来获取 java 中的当前位置(不使用任何 Android APIs)并且非常准确。到目前为止,我所拥有的是 运行 在 Java 中创建一个 HTML 文件,我想要做的是从 google API 中检索 GPS 坐标( html 个文件)。
我目前有:
HtmlRun.java
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());
}
}
findLocation.html
<!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>
当我 运行 这样做时,它会为 html 文件打开我的默认应用程序并且所有内容都已排序...但是我如何检索我需要的信息? (GPS坐标)
发生在这里:
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
但我不知道如何将该数据检索回我的 Java 应用程序。
这里是:
public static void main(String[] args) throws IOException {
File htmlFile = new File("findLocation.html");
Desktop.getDesktop().browse(htmlFile.toURI());
}
只是在网络浏览器中打开 html 文档,之后您将完全无法控制网络内容发生的情况,用户甚至可以单击地图中的所有按钮/位置而且您将永远无法访问该信息。
您需要的是一个为该应用程序的内容提供服务的网络服务器,您可以实施 REST/full 服务甚至 WebSockets 以在浏览器和服务器之间交换数据