-1

I have a simple APP that calls a HTTPS page and latitude and longitude don't work when calling https://www.mypage.com/getlonlat.html from within my APP. It works when calling page from IE, Chrome, Firefox ect. getlonlat.html has javascript that uses GeoLocation to calculate longitude and latitude. This page HAS a valid SSL.

package com.mywebsite.webview;

    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.GeolocationPermissions;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

     public class GeoWebViewActivity extends Activity {

        public class GeoWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        view.loadUrl(url);
        return true;
        }
    }


        public class GeoWebChromeClient extends WebChromeClient {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,
         GeolocationPermissions.Callback callback) {

        callback.invoke(origin, true, false);
        }
    }

        WebView mWebView;


        @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mWebView = (WebView) findViewById(R.id.webView1);

    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    mWebView.setWebViewClient(new GeoWebViewClient());

    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setGeolocationEnabled(true);
    mWebView.setWebChromeClient(new GeoWebChromeClient());

    mWebView.loadUrl("https://www.mywebsite.com");
            if (mWebView.canGoBack()) {
                mWebView.goBack();
            }


    }

       @Override
public void onBackPressed() {
    // Pop the browser back stack or exit the activity
    if (mWebView.canGoBack()) {
        mWebView.goBack();
        }
    else {
        super.onBackPressed();
        }
    }

private void postUrl(byte[] postData,
                     String url) {  mWebView.goBack();

}

}

Index.html

    <script>
        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition);
            }

        }
        function showPosition(position) {
            document.getElementById("getlat").value = position.coords.latitude;
            document.getElementById("getlon").value = position.coords.longitude;
        }


    </script>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ricky T
  • 243
  • 1
  • 13
  • http://stackoverflow.com/questions/5329662/android-webview-geolocation – adeneo Oct 13 '16 at 00:57
  • I'm very new at android, I reviewed the above link, but just not sure how to integrate it into my code. I get errors where ever I try to include examples – Ricky T Oct 13 '16 at 01:43
  • here is a good guide i used some years ago which works -- https://turbomanage.wordpress.com/2012/04/23/how-to-enable-geolocation-in-a-webview-android/ – Tasos Oct 13 '16 at 01:45
  • Tasco, that link worked except one thing. You have to use the phone back button. If I use any link within the web site, I lose GPS permissons in the app. Any Idea? I have a search based on lon lat, then search again button on web page. My search again button doesn't work as I lose my latitude and logitude – Ricky T Oct 13 '16 at 03:23
  • Sorry Tasos, not Tasco – Ricky T Oct 13 '16 at 03:25
  • I edited my existing source with info from link supplied by Tasos. Works fine when I use the android back button. But if you click the home"index.html" link button it looses longitude and latitude, even though this is the page with the javascript that accually calculates those values. Once again, works fine in IE, Chrome, Firefox and other browsers. – Ricky T Oct 13 '16 at 07:42

1 Answers1

0

As it turns out, my SSL certificate was not set up properly with my hosting company. The code above works perfectly now. How nifty, call a HTML, PHP, JAVA page from an APP without having to re-write the whole page in android.

Ricky T
  • 243
  • 1
  • 13
  • Is there any downside of just calling an entire existing website this way? Are there any advantages of re-writing my website in android? My only guess would be speed possibly. – Ricky T Oct 16 '16 at 19:25