WebView control in Android supports geographic location method

  • 2021-09-24 23:40:49
  • OfStack

Android WebView from assets to load html5 page, geographical location positioning, friends in need can refer to the following.

Today, I investigated the positioning problem of an html5 page, and found that html5 can be positioned on the mobile browser, but it cannot be positioned in webview. But I actually thought that the geographical positioning of html5 was not feasible in webview.

The html5 page reads as follows:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 </head>
 <body>
  <p id="demo"> Click this button to get your coordinates: </p>
  <button onclick="getLocation()"> Try 1 Under </button>
  <script>
   var x=document.getElementById("demo");
   function getLocation()
   {
    if (navigator.geolocation)
    {
     navigator.geolocation.watchPosition(showPosition);
    }
    else{
     x.innerHTML="Geolocation is not supported by this browser.";
    }
   }

   function showPosition(position)
   {
    x.innerHTML="Latitude: " + position.coords.latitude +
"<br />Longitude: " + position.coords.longitude; 
   }
  </script>
 </body>
</html>

Later, I checked it on the Internet and found that I need to set up something. Set the properties of websetting:


webView.setWebViewClient(new WebViewClient());
//webView.loadUrl("http://news.baidu.com/");
webView.loadUrl("file:///android_asset/index.html");


WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);


/**
*  The following parts can be dispensed with 
*/
// // Enable the database  
// webSettings.setDatabaseEnabled(true); 
// String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
//
// // Enable Geolocation  
// webSettings.setGeolocationEnabled(true); 
// // Set the database path to locate  
// webSettings.setGeolocationDatabasePath(dir); 


/**
*  This is very important, and it must be 
*/
//*** The most important method, 1 It must be set, which is the main reason why it can't get out 
webSettings.setDomStorageEnabled(true);
webView.setWebChromeClient(new WebChromeClient(){

// Configure permissions (also in the WebChromeClient Implement in) 
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
Callback callback) {
 callback.invoke(origin, true, false); 
 super.onGeolocationPermissionsShowPrompt(origin, callback);
}

});

//Turn on permissions in Androidmanifest. xml


<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

In some mobile phones that restrict the application positioning authority, it is necessary to open the application positioning authority, otherwise the positioning will fail

Problem solved!

Special try:

Open the network and GPS at the same time to locate, and get latitude and longitude information.

You can locate only by opening the network, and you can locate only by opening GPS.

Closing the network and GPS can also locate.

It can be seen that it is obtained by geographical location, and only latitude and longitude information can be obtained.

To get detailed address information, you need to call the map API implementation.


Related articles: