【问题标题】:Geolocation is not working on Android webview地理定位不适用于 Android webview
【发布时间】:2017-08-02 10:35:40
【问题描述】:

我的网站上有地理位置 - 我可以使用 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");
        }
    }

它在网络上运行良好。不适用于 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>

【问题讨论】:

  • 您是否检查了浏览器的权限?它允许定位吗?
  • 是的,

标签: javascript android google-maps webview geolocation


【解决方案1】:

试试这个代码:

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" />

【讨论】:

  • 对不起,我粗心了:))
  • 清除缓存使用的webview:webview.clearCache(true); webview.clearHistory();确保你打开手机上的gps..
  • 现在显示为空。 :( -> 2.1m.yt/Q7b_SpH.png - 截图
  • 我认为现在问题在于您的javascript 代码....对于移动设备webview 它应该可以工作。 .
  • onGeolocationPermissionsShowPrompt 应该用@Override注释
【解决方案2】:
    webview.setWebChromeClient(new WebChromeClient() {
     public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
       }
    });

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 2011-07-16
    • 2016-08-05
    • 2013-04-14
    • 2012-05-24
    • 1970-01-01
    • 2015-06-27
    相关资源
    最近更新 更多