1

I want to load Geolocation into my webview on android 2.1. The geolocation works well on my browser, but it does fail on webview.

public class WebPageLoader extends Activity implements GeolocationPermissions.Callback{

    WebView webview;
    String geoWebsiteURL = "http://hngu.naf.cs.hut.fi/current_location.html";
    public WebPageLoader() {
    }

    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webview.getSettings().setGeolocationEnabled(true);  //seems like if i set this, the webview should prompt when I call navigator.geolocation.getCurrentPosition
        GeoClient geo = new GeoClient();
        webview.setWebChromeClient(geo);        
        String origin = ""; //how to get origin in correct format?
        geo.onGeolocationPermissionsShowPrompt(origin, this);  //obviously not how this is meant to be used but expected usage not documented
        webview.loadUrl(geoWebsiteURL);        

    }

    public void invoke(String origin, boolean allow, boolean remember) {

    }

    final class GeoClient extends WebChromeClient {

    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,
    Callback callback) {
    // TODO Auto-generated method stub
    super.onGeolocationPermissionsShowPrompt(origin, callback);
    callback.invoke(origin, true, false);
    }

    }

    }

My manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="net.Webview_Android"
      android:versionCode="1"
      android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-library
        android:name="com.google.android.maps" />  
    <uses-permission android:name="android.permission.ACCESS_GPS" />
    <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
    <uses-sdk android:minSdkVersion="7" /> 
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".WebPageLoader"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

When I run the app. It display the google map with the marker. However, it cannot find the current location even though my html file works well. Please help me. Thank you so much.

lovesunset21
  • 11
  • 1
  • 3

1 Answers1

3
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  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(origin, true, false);
      }
  });
  webview.loadUrl(geoWebsiteURL);                    
}

works for me.

I use the permissions:

<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" />

Note, that on the Android emulator you have to set a location with geo fix.

ZehnVon12
  • 4,026
  • 3
  • 19
  • 23