地理定位不适用于 Android webview

sno*_*lax 1 javascript android google-maps geolocation webview

我的网站上有地理定位 - 我可以使用 javascript 获取位置,但我无法在 android 设备上使用 web 视图获取位置。我使用相同的网站,但它不起作用。

我的网站:https : //www.ardakazanci.thecompletewebhosting.com/petsHelper/paylasim.php

我的安卓代码:

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");
        }
    }
Run Code Online (Sandbox Code Playgroud)

它在网络上运行良好。不适用于 WebView。谢谢你。

错误:

地理定位服务失败

我的 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>
Run Code Online (Sandbox Code Playgroud)

raf*_*007 7

试试这个代码:

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");
    }
Run Code Online (Sandbox Code Playgroud)

还要确保您在清单中设置了权限:

 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)