title: How to use webview.loadURL to get a variable from javascript (Android)
body: <p>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. </p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>Here is my the declaration of my webview in my java code.</p>
  
  <pre><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());
  </code></pre>
  
  <p>Here is the WebAppInterface code</p>
  
  <pre><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);
  
    }
  }
  </code></pre>
  
  <p>And last but not least, here is my HTML code</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;body&gt;        
      &lt;script type="text/javascript"&gt;
  
          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});
          }   
      &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>As of now, each time I want to gather a coordinate of the user I am using the following line of code</p>
  
  <pre><code>wv.loadUrl("file:///android_asset/CurrentLocationCoordinates.html");
  </code></pre>
  
  <p>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</p>
  
  <pre><code>wv.loadUrl("javascript:storePosition()");
  </code></pre>
  
  <p>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!</p>
  
  <p>Thank you again for your time and your suggestions. </p>