問題描述
我對 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 坐標的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!