title: Android WebView geolocation
body: <p>I know this question has been asked a lot of time but i could not find any help.</p>
  
  <p>I am trying to built an app that loads a website that uses geolocation.</p>
  
  <p>When the app loads, it should open up a google map with my location on it. On a web browser it works fine.</p>
  
  <p>link i am trying to access is <a href="https://pas.irsglobal.net/test/" rel="nofollow noreferrer">https://pas.irsglobal.net/test/</a></p>
  
  <p>My code is as follows.</p>
  
  <p>//MainActivity.java</p>
  
  <pre><code>package za.co.opexsolutions.www.panicapp;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  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 {
  /**
   * WebViewClient subclass loads all hyperlinks in the existing WebView
   */
  public class GeoWebViewClient extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          // When user clicks a hyperlink, load in the existing WebView
          view.loadUrl(url);
          return true;
      }
  }
  
  /**
   * WebChromeClient subclass handles UI-related calls
   * Note: think chrome as in decoration, not the Chrome browser
   */
  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);
      }
  }
  
  
  
  WebView wv;
  String mypage = "https://pas.irsglobal.net/test/";
  String mypage_error = "file:///android_asset/mypage_error/index.html";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
      wv  = (WebView) findViewById(R.id.wv);
      wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
      wv.setWebViewClient(new GeoWebViewClient());
      wv.getSettings().setJavaScriptEnabled(true);
      wv.getSettings().setGeolocationEnabled(true);
      wv.setWebChromeClient(new GeoWebChromeClient());
      wv.setFocusable(true);
      wv.setFocusableInTouchMode(true);
      wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
      wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
      wv.getSettings().setDomStorageEnabled(true);
      wv.getSettings().setDatabaseEnabled(true);
      wv.getSettings().setAppCacheEnabled(true);
  
  
  
      wv.setWebChromeClient(new WebChromeClient() {
          public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
              // callback.invoke(String origin, boolean allow, boolean remember);
              callback.invoke(origin, true, false);
          }
      });
  
      wv.loadUrl(mypage);
  
      wv.setWebViewClient(new WebViewClient(){
          public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
              wv.loadUrl(mypage_error);
          }
      });
  
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>//Manifest.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"&gt;&lt;/uses-permission&gt;
  &lt;uses-permission android:name="android.permission.CALL_PHONE"&gt;&lt;/uses-permission&gt;
  &lt;uses-permission android:name="android.permission.LOCATION_HARDWARE"&gt;&lt;/uses-permission&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"&gt;&lt;/uses-permission&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/shipping"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/shipping"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
  
      &lt;activity android:name=".HomeActivity"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name=".MainActivity"&gt;&lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>