1. Are you ready for the Galaxy S20? Here is everything we know so far!

HTML5 Geolocation not working in Android Webview

Discussion in 'Android Development' started by Debsmita Paul, Jan 17, 2020.

  1. Debsmita Paul

    Debsmita Paul Lurker
    Thread Starter

    I am currently building a webview application for android using android studio. The website that i am trying to wrap within a webview implements a simple geolocation system that tracks the current location of the user and stores latitude and longitude.

    I am able to get the desired result in desktop browser and in mobile browser, but not in webview. I have tried some of the solutions on other forums, but nothing seems to solve this.

    The project is built in AngularJS 1.4.8. I am not using any framework like ionic or framework7. To get the location, I have used HTML5 Geolocation.



    My AngularJS Code to get current location (works fine in browser):
    Code (Text):
    1. $scope.getLocation = function () {
    2.         if(navigator.geolocation){
    3.                 navigator.geolocation.getCurrentPosition(showPosition, showError);
    4.                 //alert("work");
    5.  
    6.             } else {
    7.                 x.innerHTML = "Geolocation is not supported by this browser.";
    8.                 alert("Geolocation is not supported by this browser");
    9.             }
    10.         }
    11.  
    12.          function showPosition(position) {
    13.             alert("position");
    14.                var latitude_val ,longitude_val ;
    15.                latitude_val = position.coords.latitude ;
    16.                longitude_val = position.coords.longitude ;
    17.                alert("lat"+latitude_val + "lon"+longitude_val)
    18.             }
    19.  
    20.             function showError(error) {
    21.                 var error;
    22.                 switch(error.code) {
    23.                     case error.PERMISSION_DENIED:
    24.                     error = "User denied the request for Geolocation."
    25.                     break;
    26.                     case error.POSITION_UNAVAILABLE:
    27.                     error = "Location information is unavailable."
    28.                     break;
    29.                     case error.TIMEOUT:
    30.                     error = "The request to get user location timed out."
    31.                     break;
    32.                     case error.UNKNOWN_ERROR:
    33.                     error = "An unknown error occurred."
    34.                     break;
    35.                 }
    36.  
    37.                 alert(error);
    38.             }
    39.  


    Android Studio code (MainAcivity.java):
    Code (Text):
    1. public class MainActivity extends AppCompatActivity {
    2.  
    3. private WebView webView;
    4.  
    5. [USER=1021285]@override[/USER]
    6. protected void onCreate(Bundle savedInstanceState) {
    7.     super.onCreate(savedInstanceState);
    8.  
    9.     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    10.  
    11.     setContentView(R.layout.activity_main);
    12.  
    13.     webView =(WebView)findViewById(R.id.webview);
    14.     webView.setWebViewClient(new WebViewClient());
    15.  
    16.     WebSettings webSettings = webView.getSettings();
    17.  
    18.     webView.getSettings().setJavaScriptEnabled(true);
    19.     webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    20.     webView.getSettings().setGeolocationEnabled(true);
    21.     webView.getSettings().setGeolocationDatabasePath( getFilesDir().getPath() );
    22.  
    23.     // HTML5 API flags
    24.     webView.getSettings().setAppCacheEnabled(true);
    25.     webView.getSettings().setDatabaseEnabled(true);
    26.     webView.getSettings().setDomStorageEnabled(true);
    27.  
    28.  
    29.     webView.setWebChromeClient(new WebChromeClient() {
    30.         public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
    31.             callback.invoke(origin, true, false);
    32.         }
    33.     });
    34.     webView.loadUrl("https://my.url");
    35. }
    36.  




    Android Mainfest Permissions:
    Code (Text):
    1. <uses-permission android:name="android.permission.INTERNET" />
    2. <uses-permission android:name="android.permission.ACCESS_GPS" />
    3. <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
    4. <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    5. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    6. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    7.  
    It seems like the position parameter is always null but I cannot figure out why. Please help.
     


    #1 Debsmita Paul, Jan 17, 2020
    Last edited by a moderator: Jan 17, 2020
    racibennett likes this.

    1. Download the Forums for Android™ app!


      Download

       
  2. Unforgiven

    Unforgiven ...eschew obfuscation...
    Moderator

    I moved this to the developer area for better exposure. I also added code tags (example below) to make it more readable. Good luck. :)

    Code Tags Usage:
    [code]Line 1
    Line 2
    Line 3
    [/code]

    Result:
    Code (Text):
    1. Line 1
    2. Line 2
    3. Line 3
    4.  
     
Loading...
Similar Threads - HTML5 Geolocation working
  1. ivanburyy1r64rewtr
    Replies:
    4
    Views:
    286
  2. ujur007
    Replies:
    1
    Views:
    234
  3. The_Chief
    Replies:
    11
    Views:
    449
  4. Mobile Visuals
    Replies:
    0
    Views:
    242
  5. Captainstrat
    Replies:
    6
    Views:
    334
  6. PAB61
    Replies:
    2
    Views:
    356
  7. justamel
    Replies:
    2
    Views:
    272
  8. DrAbst
    Replies:
    1
    Views:
    392
  9. Ruben u Williams
    Replies:
    1
    Views:
    448
  10. chinnar
    Replies:
    0
    Views:
    1,880

Share This Page

Loading...