2

I have a very, very simple scenario and I am running into a very common issue (noted by many StackOverflow users over time). However, I have tried every proposed solution, and none of them seem to work. Can somebody please help to resolve this question once and for all?

I am building an Android app (for Android 7, let's assume) which contains a Webview which points to a website that implements very simple geolocation. (My latest, very stripped down version of the website simply displays the user's latitude and longitude.)

I can view the site fine (and it displays latitude/logitude perfectly) in Chrome. But when I run the app using Webview, nothing is displayed.

Over the past few days, I have tried seemingly every proposed solution (in MainActivity.java) to this issue, but none of the solutions seem to work.

I have added all permissions imaginable in AndroidManifest.xml.

What does one need to do to get this to work?

Surely, viewing a website with geolocation in an Android app with Webview should be fairly straightforward, right???

Edit: I've narrowed things down a bit. I've prompted the user for permissions at runtime, and I've confirmed that geolocation is being allowed. I've also confirmed that Javascript, in general, is being allowed by my app.

The key/core remaining issue seems to be this: The following does not appear to work in Webview.

navigator.geolocation.getCurrentPosition(success, error);

I don't know why, but that is the problem.

Ref:

Android webview error when asking for geolocation

The application does not have sufficient geolocation permissions

Location is accessed in Chrome, doesn't work in WebView

Android Ask for permission to use location within webview

Minimal geolocation webpage: (How to get this to display in Android Webview?)

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Webview Test</title>

</head>
<body>

<div id='output'></div>

<script>

function success(pos) {

  var latitude = pos.coords.latitude;
  var longitude = pos.coords.longitude;

  document.getElementById('output').innerHTML = `Your current position is:  Latitude : ${latitude} Longitude: ${longitude}`;
}

function error(err) {
  console.warn('ERROR(${err.code}): ${err.message}');
}

navigator.geolocation.getCurrentPosition(success, error);

</script>

</body>
</html>
  • Welcome to Stack Overflow! Welcome to Stackoverflow.com! Please make your question a Minimal, Complete, and Verifiable example [M.C.V E](https://stackoverflow.com/help/mcve). Also check [how to ask question](https://stackoverflow.com/help/how-to-ask) to make your post answerable. – Morse Jun 04 '18 at 21:53

1 Answers1

0

Create a class JavaScriptReceiver.java

public class JavaScriptReceiver
{
Context mContext;
/** Instantiate the receiver and set the context*/
JavaScriptReceiver(Context c) {
mContext = c;
}
@JavascriptInterface
public void showShopping(){
Intent intent = new Intent(mContext, ShoppingActivity.class);
mContext.startActivity(intent);
}
@JavascriptInterface
public void showOrders(int orderid){
Intent intent = new Intent(mContext, MyOrdersActivity.class);
intent.putExtra("order",orderid);
mContext.startActivity(intent);
}
}

Then call addjavascriptinterface() interface through object of WebView.

JavaScriptReceiver javaScriptReceiver;
javaScriptReceiver = new JavaScriptReceiver(this);
webView.addJavascriptInterface(javaScriptReceiver, "JSReceiver");
CodeChimp
  • 4,745
  • 6
  • 45
  • 61
Parth Patel
  • 859
  • 2
  • 11
  • 28