Jim Papas
Jim Papas

Reputation: 31

Geolocation not working on webview android

I 'm trying to create an application where it will find the location of a user and it will show his location on a map. I have read the other articles about this issue and the suggested solutions but nothing worked for me. This Web app runs good in browsers (mobile and pc) but when I am trying to run it as an application stacks in the javascript of html file. I think that is a problem of permissions in javascript. This is the code of this 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 my location</button> 
        </div> 
      </body> 
    </html>  

I 've spend several hours of searching and trying suggestion from the web but nothing worked. If someone knows something more about this issue please help.

Thanks a lot.

Upvotes: 2

Views: 8274

Answers (1)

vimal1083
vimal1083

Reputation: 8671

Bro, I too faced the same problem , i fixed by adding these below lines.hope you will also

 webView.getSettings().setAppCacheEnabled(true);
 webView.getSettings().setDatabaseEnabled(true);
 webView.getSettings().setDomStorageEnabled(true);

Upvotes: 1

Related Questions