Reputation: 65
I have designed a simple browser for Android using WebView
. Everything is working fine but when I open Google maps then my browser can't access the current location of the device. I don't understand what is going wrong.
also in manifest i have given the permission ACCESS FINE LOCATION
My code is:
@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView ourBrow=(WebView)findViewById(R.id.wvBrowser);
Button bgo=(Button)findViewById(R.id.button3);
Button res=(Button)findViewById(R.id.button4);
Button gfo=(Button)findViewById(R.id.button2);
ourBrow.getSettings().setJavaScriptEnabled(true);
try{
ourBrow.loadUrl("https://www.google.co.in/maps/dir///@20.3464436,85.8127819,15z");
}
catch(Exception e)
{
e.printStackTrace();
/*String myHtml = "<html><body>A very basic <b>WebView</b> demo!</body></html>";
ourBrow.loadData(myHtml, "text/html", null);*/
}
bgo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(ourBrow.canGoBack())
ourBrow.goBack();
}
});
gfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(ourBrow.canGoForward())
ourBrow.goForward();
}
});
res.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ourBrow.loadUrl("https://www.google.co.in/maps/dir///@20.3464436,85.8127819,15z");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 2
Views: 5223
Reputation: 3770
WebView
, using
WebSettings.setJavaScriptEnabled(true);
The app needs permission
ACCESS_FINE_LOCATION The WebView
must use a custom WebChromeClient
which implements
WebChromeClient.onGeolocationPermissionsShowPrompt()
. This method
is called by the WebView to obtain permission to disclose the user's
location to JavaScript. (In the case of the browser, we show a prompt
to the user.) The default implementation does nothing, so permission
is never obtained and the location is never passed to JavaScript.webView.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
Geolocation uses databases to persist cached positions and permissions between sessions. The location of the database is set using WebSettings.setGeolocationDatabasePath(...). If the location of the database is not set, the persistent storage will not be available, but Geolocation will continue to function correctly otherwise. To set the location of the databases, use ...
webView.getSettings().setGeolocationDatabasePath( context.getFilesDir().getPath() );
Upvotes: 6