Current Location Android using WebView -
i have designed simple browser android using webview. working fine when open google maps browser can't access current location of device. don't understand going wrong. in manifest have given permission access fine location 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 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 menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
- javascript must enabled in
webview
, usingwebsettings.setjavascriptenabled(true);
app needs permission access_fine_locationwebview
must use customwebchromeclient
which implementswebchromeclient.ongeolocationpermissionsshowprompt()
. method called webview obtain permission disclose user's location javascript. (in case of browser, show prompt user.) default implementation nothing, permission never obtained , location never passed javascript.webview.setwebchromeclient(new webchromeclient() { public void ongeolocationpermissionsshowprompt(string origin, geolocationpermissions.callback callback) { callback.invoke(origin, true, false); } });
geolocation uses databases persist cached positions , permissions between sessions. location of database set using websettings.setgeolocationdatabasepath(...). if location of database not set, persistent storage not available, geolocation continue function correctly otherwise. set location of databases, use ...
webview.getsettings().setgeolocationdatabasepath( context.getfilesdir().getpath() );
Comments
Post a Comment