I have an Android app that uses a web view. The web view works well, except for the JS that uses getCurrentPosition to locate the user. I have read about this issue and understand that I need to have the right permissions, and that I also have to set setJavaScriptEnabled(true);
. I have done these things, but am still having a lot of trouble with my Android app. Sometimes the prompt that requests permission to obtain the user's location doesn't even come up. Can anyone help me fix this? I have included the relevant Java file, the permissions from my manifest.xml, and the JS that gets the user's location. Any help would be wonderful!
Here is my Java file:
package com.website.appname;
import android.app.Activity;
//OTHER IMPORTS HERE
class MyClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
Callback callback) {
callback.invoke(origin, true, false);
}
}
public class locationPage extends Activity {
WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.embed);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.requestFocus(View.FOCUS_DOWN);
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
webView.loadUrl("http://www.website/page.php");
webView.setWebChromeClient(new MyClient());
@SuppressWarnings("unused")
class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Here are the permissions in my manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
And here is the webpage that is loaded by the web view:
<script>
if( navigator.geolocation ) {
// Call getCurrentPosition with success and failure callbacks
navigator.geolocation.getCurrentPosition( success, fail );
}
else {
alert("Sorry, your browser does not support geolocation services.");
}
function success(position) {
// GeoPosition Object
window.location = "another_page.php?user_lat=" + position.coords.latitude
+ "&user_lon=" + position.coords.longitude + "&accuracy=" +
position.coords.accuracy;
}
function fail() {
// Could not obtain location
}
</script>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
P.S. I have also tried looking at things like android webview geolocation, which were helpful, but my issue still persists. Thank you!