Android Webview with HTML 5 geolocation

I am using webview to download a web application. In the downloadable page, I use the HTML 5 geolocation API to locate users. This works in all browsers and currently works from the iOS app. For some reason, I can't get it to work in the Android web browser. When I run the code below, I see this message in the stack trace:

[INFO: CONSOLE (24)] "Uncaught ReferenceError: PageMethods not defined", source: [URL FOR SCRIPT] (24)

Again it works great in the browser.

    package bar.krowd.krowdapp;

    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.webkit.GeolocationPermissions;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class MainActivity extends AppCompatActivity {

    private WebView KrowdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();

        KrowdView = (WebView) findViewById((R.id.activity_main_webview));

        WebSettings ws = KrowdView.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setGeolocationEnabled(true);


        KrowdView.setWebViewClient(new WebViewClient());
        KrowdView.setWebChromeClient(new GeoWebChromeClient());

        KrowdView.setBackgroundColor(Color.BLACK);
        KrowdView.loadUrl([I AM PUTTING THE URL HERE]);
}


@Override
public void onBackPressed() {
    // Pop the browser back stack or exit the activity
    if (KrowdView.canGoBack()) {
        KrowdView.goBack();
    }
    else {
        super.onBackPressed();
    }
}

public class GeoWebChromeClient extends WebChromeClient {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,
                                                   GeolocationPermissions.Callback callback) {
        // Always grant permission since the app itself requires location
        // permission and the user has therefore already granted it
        callback.invoke(origin, true, false);
    }
}

}

My AndroidManifest.xml lists the following:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+4
source share
2 answers

, , targetSdkVersion 23 21 app build.gradle

+2

:

webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
0

Source: https://habr.com/ru/post/1617153/


All Articles