[英]Geolocation is not working on Android webview
I have geolocation on my website - I can get location with javascript but I can not get location using web view on android devices.我的网站上有地理定位 - 我可以使用 javascript 获取位置,但我无法使用 android 设备上的 web 视图获取位置。 I use the same site but it does not work.
我使用相同的网站,但它不起作用。
my site: https://www.ardakazanci.thecompletewebhosting.com/petsHelper/paylasim.php我的网站: https://www.ardakazanci.thecompletewebhosting.com/petsHelper/paylasim.php
my android code:我的 android 代码:
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://www.ardakazanci.thecompletewebhosting.com/petsHelper/paylasim.php");
}
}
It works fine on the web. Does not work with WebView. Thank you.它在 web 上工作正常。不适用于 WebView。谢谢。
Error:错误:
the geolocation service failed地理定位服务失败
My javascript code - paylasim.php我的 javascript 代码 - paylasim.php
<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 outputlat = document.getElementById('latitude'); // Enlemi tutsun
var outputlng = document.getElementById('longitude'); // Boylamı tutsun
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 15
});
var infoWindow = new google.maps.InfoWindow({map: map});
// HTML 5 ile geo location olayının sağlanması
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
outputlat.value = position.coords.latitude;
outputlng.value = position.coords.longitude;
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Lokasyon bulundu.');
map.setCenter(pos);
}, function () {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Eğer browser desteklemiyor ise
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>
try this code: 试试这个代码:
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setGeolocationEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
webview.loadUrl("https://www.ardakazanci.thecompletewebhosting.com/petsHelper/paylasim.php");
}
Also make Sure u set the permission in manifest: 还确保您在清单中设置权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
webview.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
Blindly returning this is not an answer u need to check the permission before invoking the callback盲目返回这不是答案你需要在调用回调之前检查权限
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.