navigator.geolocation.getCurrentPosition() never returns in WebView on Android

后端 未结 8 1269
萌比男神i
萌比男神i 2021-02-18 16:27

I am trying to access the HTML Geolocation API available in Android WebView (using SDK version 24).

The main problem is that the call to navigator.geolocat

8条回答
  •  情话喂你
    2021-02-18 16:58

    Actually, navigator is a child of browser global object, the window. you should first access to window then call the navigator. In some modern browsers, In addition to the window, some child object is presented like a global object, for example: location, navigator and etc.

    The WebView has no global object window. So you can add it manually, for this action please read this medium article.

    Maybe your code will be like below:

    my_web_view.evaluateJavascript("javascript: " + "updateFromAndroid(\"" + edit_text_to_web.text + "\")", null);
    

    And then add JavaScript interface, like the window object, to this evaluator:

    my_web_view.addJavascriptInterface(JavaScriptInterface(), JAVASCRIPT_OBJ); //JAVASCRIPT_OBJ: like window, like navigator, like location and etc.
    

提交回复
热议问题