Geolocation not working on webview android -
i 'm trying create application find location of user , show location on map. have read other articles issue , suggested solutions nothing worked me. web app runs in browsers (mobile , pc) when trying run application stacks in javascript of html file. think problem of permissions in javascript. code of project.
java code
import android.os.bundle; import android.app.activity; import android.view.menu; import android.webkit.webchromeclient; import android.webkit.webview; import android.webkit.geolocationpermissions; public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); webview webview = (webview) findviewbyid(r.id.webview); webview.getsettings().setjavascriptenabled(true); webview.getsettings().setjavascriptcanopenwindowsautomatically(true); webview.getsettings().setgeolocationenabled(true); webview.setwebchromeclient(new webchromeclient() { public void ongeolocationpermissionsshowprompt(string origin, geolocationpermissions.callback callback) { // callback.invoke(string origin, boolean allow, boolean remember); callback.invoke(origin, true, false); } }); webview.loadurl("file:///android_asset/www/geomap.html"); } }
androidmanifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="gr.######.navigator" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="15" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_gps" /> <uses-permission android:name="android.permission.access_assisted_gps" /> <uses-permission android:name="android.permission.access_location" /> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.access_fine_location" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
html
<!doctype html> <html> <head> <script src="http://code.jquery.com/jquery-1.9.0rc1.js"></script> <script> jquery(window).ready(function(){ $("#btninit").click(function() { alert('click button'); initiate_geolocation(); }); }); function initiate_geolocation() { alert('function initiate_geolocation'); navigator.geolocation.getcurrentposition(handle_geolocation_query,handle_errors); } function handle_errors(error) { alert('function handle_errors'); switch(error.code) { case error.permission_denied: alert("user did not share geolocation data"); break; case error.position_unavailable: alert("could not detect current position"); break; case error.timeout: alert("retrieving position timed out"); break; default: alert("unknown error"); break; } } function handle_geolocation_query(position){ alert('function handle_geolocation_query'); alert('lat: ' + position.coords.latitude + ' lon: ' + position.coords.longitude); } </script> </head> <body> <div> <button id="btninit" >find location</button> </div> </body> </html>
i 've spend several hours of searching , trying suggestion web nothing worked. if knows more issue please help.
thanks lot.
bro, faced same problem , fixed adding these below lines.hope also
webview.getsettings().setappcacheenabled(true); webview.getsettings().setdatabaseenabled(true); webview.getsettings().setdomstorageenabled(true);
Comments
Post a Comment