无法在 Android 的浏览器上使用 Javascript 获取 GPS

Unable to get GPS coordinates using Javascript on browser in Android(无法在 Android 的浏览器上使用 Javascript 获取 GPS 坐标)

本文介绍了无法在 Android 的浏览器上使用 Javascript 获取 GPS 坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 android、java 和 javascript 编码非常陌生.

I am very new to android, java and javascript coding.

我正在尝试获取当前的 GPS 坐标,然后在 Android 设备上对其进行跟踪.我正在使用 webview,并且大部分代码都是用 javascript 编写的.

I am trying to get current GPS co-ordinates and track it thereafter on an Android device. I am using webview and the majority of the code is written in javascript.

这几天我进行了很多搜索并尝试了许多不同的方法,但我无法在 android 设备上获取 GPS 坐标.请查看代码并帮助我弄清楚为什么我没有获得 GPS 位置.

I have searched a lot for a few days and tried many different ways but I am unable to get GPS co-ordinates on the android device. Please have a look at the code and hel pme figure out why I am not getting the GPS location.

需要指出的几点是 -1. 我下载并检查了其他传感器应用程序,可以看到当前显示的 GPS 坐标(因此 GPS 正在设备上工作).2. 当我运行我的代码时,我在通知区域看到 GPS 搜索图标,它一直在闪烁,但从未修复.3. 当我在笔记本电脑浏览器上只运行带有 javascript 的 HTML 文件时,它会请求共享位置的权限,然后给出坐标,但是同一个文件在 android 中没有显示任何内容.

A few things to point are - 1. I downloaded and checked other sensor apps and can see the current GPS co-ordinates being shown (so the GPS is working on the device). 2. When I run my code, I see the GPS searching icon in the notification area, it keeps blinking but never fixes. 3. When I run only the HTML file with the javascript on a laptop browser, it ask permission to share location and then it gives the co-ordinates, however the same file does not show anything in android.

请查看下面的代码并帮助我解决这个问题.我已经尝试了很多,并将其发布为我最后的希望.

Please have a look at the code below and help me figure this out. I have tried a lot and am posting this as my last hope.

清单文件具有以下权限-

The manifest file has the following permissions -

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />

Android中的主要java代码有-

The main java code in Android has -

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    webView = new WebView(this);  
    webView.getSettings().setJavaScriptEnabled(true);   
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.loadUrl("file:///android_asset/mymap.html");
    setContentView(webView);
}

而带有 javascript 的 HTML 文件是 -

And the HTML file with the javascript is -

<!DOCTYPE html>
<html>
<body onunload="OnUnload()" onload="getLocation()">
<p id="demo">Your coordinates:</p>
<script>
var watchID;
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
       var options = {maximumAge:600000, timeout:100000, enableHighAccuracy: true};
       watchID = navigator.geolocation.watchPosition(showPosition,showError,options);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude + 
  "<br />Longitude: " + position.coords.longitude;  
  }
function showError(error)
  {
  switch(error.code) 
    {
    case error.PERMISSION_DENIED:
      x.innerHTML="User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML="Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML="The request to get user location timed out."
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML="An unknown error occurred."
      break;
    }
  }

function OnUnload()
{
   alert ("The current document will be unloaded!");
   navigator.geolocation.clearWatch(watchID);
}  
</script>
</body>
</html>

提前非常感谢,斧头

推荐答案

好吧,我做了进一步的搜索并找到了解决方案.我在此处发布这些链接,作为对我的问题的回答,供任何前来这里寻找答案的人使用.

Well I did further search and found a solution. I am posting these links here as answer to my question for anyone who comes till here looking for answers.

在加载 url 之前,我必须在我的 java 代码中添加这些行,然后我才能看到地理坐标.

I had to add these lines in my java code before loading the url and then I was able to see the geo-coordinates.

    webView.getSettings().setGeolocationEnabled(true);
    webView.setWebChromeClient(new WebChromeClient() {
         public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            // callback.invoke(String origin, boolean allow, boolean remember);              
            callback.invoke(origin, true, false);
         }
        });

更多信息请点击这两个链接-

For more information follow these two links -

  • android webview 地理位置
  • Android:使用 html5 确定地理位置在 webview 中使用 javascript api

这篇关于无法在 Android 的浏览器上使用 Javascript 获取 GPS 坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:无法在 Android 的浏览器上使用 Javascript 获取 GPS

基础教程推荐