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.