0

I'm going to try to make this as clear as I can. I'm normally not the best at asking clear questions, so thank you in advanced for reading this and posting any suggestions.

I'm writing a simple android application that requires the users position. I am using a webview along side with the HTML navigator.geolocation.getCurrentPosition ability to track the user.

The issue I'm having is getting the coordinates gathered in the HTML file into my android/java application. I'm using a javascriptInterface within my webview to communicate between the two.

Here is my the declaration of my webview in my java code.

//Create the web-view
final WebView 
wv = (WebView) findViewById(R.id.webview);  
wv.getSettings().setJavaScriptEnabled(true);
//Creates the interface "Android". This class can now be referenced in the HTML file.
wv.addJavascriptInterface(new WebAppInterface(this),"Android");
wv.setWebChromeClient(new WebChromeClient());

Here is the WebAppInterface code

public class WebAppInterface extends Activity 
 {
  Context mContext;

  /** Instantiate the interface and set the context */
  WebAppInterface(Context c) {mContext = c;}

  //Used to display Java Toast messages for testing and debugging purposes 
  @JavascriptInterface
  public void showToast(String toast) {
      Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
 }

  @JavascriptInterface
  public void storeCoord(String myLat,String myLong) 
  {
    Log.d("Coordinate Log","HTML call to storeCoords()");

    //Log.d("Coord Lat", myLat);
    //Log.d("Coord Long", myLong);

  }
}

And last but not least, here is my HTML code

<!DOCTYPE html>
<html>
  <body>        
    <script type="text/javascript">

        function storePosition(position) 
        {
            Android.showToast("In storePosition()");                                        

            myLat = position.coords.latitude;
            myLong = position.coords.longitude;

            Android.showToast(myLat +" "+ myLong);  
                            Android.storeCoord(myLat,myLong);               
        }

        function fail(error){}  

        if (navigator.geolocation) 
        {
            navigator.geolocation.getCurrentPosition(storePosition,fail,{enableHighAccuracy:true,timeout:10000});
        }   
    </script>
</body>

As of now, each time I want to gather a coordinate of the user I am using the following line of code

wv.loadUrl("file:///android_asset/CurrentLocationCoordinates.html");

That works fine, up until the storePosition function is called. I figured it was something to do with the fact that I'm not specifically calling the storePosition function from the loadURL, so I tried

wv.loadUrl("javascript:storePosition()");

Which works. But!!... I don't have the gathered position from the navigator.geolocation.getCurrentPosition to send to the function storedPosition, so obviously nothing happens. I've searched everywhere on the web for a solution and I came to the conclusion that I simply do not understand how the webview.LoadURL function works. I need to get the coordinate stored in my android application!

Thank you again for your time and your suggestions.

user2278414
  • 1
  • 1
  • 2
  • Is there a particular reason you are going through these gyrations, rather than just using `LocationManager` from your Java code? – CommonsWare Apr 21 '13 at 21:32
  • This is the first time I've ever had to deal with getting the users location, let alone my first time writing a android application, so my answer to that is no...I was told this was a simple way to do things. I never looked into the LocationManager. – user2278414 Apr 21 '13 at 21:39
  • 2
    Gadzooks! What you're doing is like trying to figure out how many eggs are left in the carton by smashing the carton repeatedly with a hammer, then estimating the amount of stuff leaking out. :-) Read http://developer.android.com/guide/topics/location/index.html and http://developer.android.com/guide/topics/location/strategies.html and http://developer.android.com/training/basics/location/index.html and http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html – CommonsWare Apr 21 '13 at 21:41
  • I'll stop measuring the leakage amount and read those articles. Thanks much :) – user2278414 Apr 21 '13 at 21:43

0 Answers0