从已启动的HTML文件中将数据检索到Java Application中

时间:2017-03-14 16:31:20

标签: java geolocation gps location geocoding

所以我一直在寻找一种方法来获取java中的当前位置(不使用任何Android API)以及非常准确的东西。到目前为止,我在Java中运行HTML文件,我想要做的是从谷歌API(html文件)中检索GPS坐标。

到目前为止我有什么:

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应用程序。

1 个答案:

答案 0 :(得分:0)

这里:

public static void main(String[] args) throws IOException {
    File htmlFile = new File("findLocation.html");
    Desktop.getDesktop().browse(htmlFile.toURI());
}

只是在网络浏览器中打开html文档,之后你无法控制所有关于网页内容的内容,用户甚至可以点击地图中的所有按钮/ pois,你永远无法访问那个信息。

您需要的是一个服务于该应用程序内容的Web服务器,您可以实现REST /完整服务甚至WebSockets以在浏览器和服务器之间交换数据