地理定位在Android webview上不起作用

velaa5lx  于 2022-12-02  发布在  Android
关注(0)|答案(2)|浏览(155)

我的网站上有地理位置-我可以用javascript获得位置,但我不能在android设备上使用网络视图获得位置。我使用同一个网站,但它不起作用。
我的网站: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");
        }
    }

它在网络上工作得很好。不能与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>
j8ag8udp

j8ag8udp1#

请尝试以下代码:

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");
    }

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
xxhby3vn

xxhby3vn2#

webview.setWebChromeClient(new WebChromeClient() {
     public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
       }
    });

盲目地返回这不是一个答案,您需要在调用回调之前检查权限

相关问题