无法GPS坐标使用Javascript的浏览器在Android中获得 [英] Unable to get GPS coordinates using Javascript on browser in Android

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

问题描述

我很新到Android,Java和JavaScript编码。

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

我试图让目前的GPS坐标,然后跟踪它在Android设备上。我使用的WebView和广大的code是用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.

我寻觅了很多了几天,并尝试了许多不同的方式,但我无法获得GPS坐标在Android设备上。请看看在code和HEL PME弄清楚为什么我没有收到的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.当我运行我的code,我看到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.

请看看下面的code和帮助我想出解决办法。我已经尝试了很多,我张贴这是我最后的希望。

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" />

在主要的Java code在Android有 -

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>

感谢很多提前, AXS

Thanks a lot in advance, axs

推荐答案

嗯,我没有进一步的搜索,并找到了解决办法。我在这里张贴这些链接作为回答我的问题的人谁上台到这里寻找答案。

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 code这几行,然后我能看到的地理坐标。

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 -

  • 的Andr​​oid的WebView地理位置
  • <一个href="http://stackoverflow.com/questions/2390641/android-using-html5-to-determine-geolocation-in-webview-with-javascript-api">Android:使用HTML5以确定的JavaScript API
  • 地理位置在web视图
  • android webview geolocation
  • Android: Using html5 to determine geolocation in webview with javascript api

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆