地理定位不适用于Android webview

时间:2017-03-12 10:22:05

标签: javascript android google-maps webview geolocation

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

1 个答案:

答案 0 :(得分:3)

试试这段代码:

'a

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

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