Android WebView loads html5 pages from assets to implement geolocation. Friends in need can refer to it.
Today I investigated the positioning problem of an html5 page and found that html5 can be positioned on a mobile browser, but it cannot be positioned in a webview. And I actually think that the geographical positioning of html5 is not feasible in webview.
The content of the html5 page is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <p >Click this button,Get your coordinates:</p> <button onclick="getLocation()">Try it</button> <script> var x=("demo"); function getLocation() { if () { (showPosition); } else{ ="Geolocation is not supported by this browser."; } } function showPosition(position) { ="Latitude: " + + "<br />Longitude: " + ; } </script> </body> </html>
Later, I checked online and found that I needed to set up some things. Set the properties of the websetting:
(new WebViewClient()); //("/"); ("file:///android_asset/"); WebSettings webSettings = (); (true); /** * The following parts are not allowed */ // //Enable database// (true); // String dir = ().getDir("database", Context.MODE_PRIVATE).getPath(); // // //Enable geolocation// (true); // // Set the database path for positioning// (dir); /** * It's important here, it must be */ //***The most important method must be set, this is the main reason why it cannot be released(true); (new WebChromeClient(){ //Configure permissions (also implemented in WebChromeClient)@Override public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) { (origin, true, false); (origin, callback); } });
//Enable permissions in
<uses-permission android:name="" /> <uses-permission android:name=".ACCESS_FINE_LOCATION" /> <uses-permission android:name=".ACCESS_COARSE_LOCATION" />
In some mobile phones that restrict application positioning permissions, the application positioning permissions need to be enabled, otherwise the positioning will fail
Problem solved!
Specially tried:
Open the network and GPS at the same time to locate, and what you get is latitude and longitude information.
Only the network can be opened to locate, only the GPS can be opened to locate.
Turn off the network and GPS can also be located.
It can be seen from geographical location positioning that can only obtain latitude and longitude information.
To obtain detailed address information, you need to call the map API implementation.
The above article on the method of supporting geolocation positioning of WebView controls in Android is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.