InspiredGhost
InspiredGhost

Reputation: 135

Android WebView geolocation

I know this question has been asked a lot of time but i could not find any help.

I am trying to built an app that loads a website that uses geolocation.

When the app loads, it should open up a google map with my location on it. On a web browser it works fine.

link i am trying to access is https://pas.irsglobal.net/test/

My code is as follows.

//MainActivity.java

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);
        }
    });



}

}

//Manifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.LOCATION_HARDWARE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@mipmap/shipping"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/shipping"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".HomeActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity"></activity>
</application>

Upvotes: 1

Views: 2358

Answers (1)

InspiredGhost
InspiredGhost

Reputation: 135

OK, After a some time of searching what could be the problem, I hope this will help others too. The problem in this case it is not my code, but the Android version i was running on the emulator. (I was using Android 7 Nougat).

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.

For full assistance Please Click Here

Upvotes: 1

Related Questions