【问题标题】:Retrieve data into Java Application from an HTML file that was launched从已启动的 HTML 文件中将数据检索到 Java 应用程序中
【发布时间】:2017-03-14 16:31:20
【问题描述】:

所以我一直在寻找一种在 Java 中获取当前位置的方法(不使用任何 Android API)以及一些非常准确的方法。到目前为止,我在 Java 中运行一个 HTML 文件,而我想做的是从 google 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 应用程序。

【问题讨论】:

  • 哇,甚至没有关闭......你需要一个服务器来SERVES一个请求,并且当你的客户端做某事并将信息发送回服务器时......您可以搜索/阅读有关 RESTfull 服务或 WebSockets....
  • @ΦXocę웃Пepeúpaツ 有没有办法在java中获取当前位置?使用任何 Google api?

标签: java geolocation gps location geocoding


【解决方案1】:

这里是:

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

只是在网络浏览器中打开 html 文档,之后您完全无法控制网络内容发生的情况,用户甚至可以单击地图中的所有按钮/点,您将永远无法访问到那个信息。

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

【讨论】:

    猜你喜欢
    • 2012-12-18
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 2018-09-01
    • 2012-03-26
    • 1970-01-01
    • 2021-09-01
    相关资源
    最近更新 更多