我正在编写一个获取用户位置并将其发送到服务器的应用程序。我在服务器端使用JavaScript和PHP。我正在尝试将此应用加载到android Webview上,并且它不会将位置发送到JavaScript,因此可以通过ajax将位置发送到服务器,但是当我使用Chrome在移动设备上运行该位置时,它可以正常工作。请我需要知道我做的不正确。我的android代码粘贴在下面
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebview = (WebView) findViewById(R.id.webview);
WebSettings websettings = myWebview.getSettings();
myWebview.setWebViewClient(new WebViewClient());
websettings.setJavaScriptEnabled(true);
websettings.setJavaScriptCanOpenWindowsAutomatically(true);
websettings.setGeolocationDatabasePath( getFilesDir().getPath() );
myWebview.setWebChromeclient(new WebChromeclient() {
public void onGeolocationPermissionsShowPrompt(String origin,GeolocationPermissions.Callback callback) {
callback.invoke(origin,true,false);
}
});
websettings.setappCacheEnabled(true);
websettings.setDatabaseEnabled(true);
websettings.setDomStorageEnabled(true);
myWebview.loadUrl("https://example.com/proj/");
}
我已经在Android清单文件中获得了必要的权限。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.accESS_FINE_LOCATION" />
下面的代码是我处理位置的JavaScript
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getcurrentPosition(reportLocation,showError);
} else {
$$('locationFeedbackText').innerHTML = "Geolocation is not supported by this Devide.";
}
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
$$('locationFeedbackText').innerHTML = "User denied the request for Geolocation.";
break;
case error.POSITION_UNAVAILABLE:
$$('locationFeedbackText').innerHTML = "Location information is unavailable.";
break;
case error.TIMEOUT:
$$('locationFeedbackText').innerHTML = "The request to get user location timed out.";
break;
case error.UNKNOWN_ERROR:
$$('locationFeedbackText').innerHTML = "An unknown error occurred.";
break;
}
}
当我单击用户界面上的按钮时,应该将用户位置发送到服务器并显示成功消息,但是显示的输出消息是来自showError函数“位置不可用”的消息。 请记住,此应用在我的移动设备上加载了chrome后即可正常运行。