ΓDΛ
ΓDΛ

Reputation: 11110

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. I use the same site but it does not work.

my site : https://www.ardakazanci.thecompletewebhosting.com/petsHelper/paylasim.php

my android code :

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.

Error:

the geolocation service failed

My javascript code - 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>

Upvotes: 1

Views: 11646

Answers (2)

Munna
Munna

Reputation: 21

    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

Upvotes: 1

rafsanahmad007
rafsanahmad007

Reputation: 23881

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

Upvotes: 9

Related Questions