I am currently facing an issue where I am unable to display the latitude and longitude values on a webview by accessing a Java method from JavaScript. The values are not getting displayed as expected.
Any help or guidance in resolving this issue would be greatly appreciated. Thank you!
Below is my WebviewActivity.java code snippet:
static final String TAG = "JavascriptDataDemo";
DataHelper myDB=new DataHelper(this);
/** This method retrieves data for JS */
@JavascriptInterface
public String getData() {
// Inserting dummy latitude and longitude values into database
myDB.insert(16.5048, 80.6338);
myDB.insert(16.5024, 80.6432);
myDB.insert(16.512, 80.6216);
myDB.insert(16.5124, 80.6219);
Cursor cursor = myDB.fetchAllCountries();
double[] array = new double[cursor.getCount()];
double[] array1 = new double[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
double data = cursor.getDouble(cursor.getColumnIndex("lat"));
double data1 = cursor.getDouble(cursor.getColumnIndex("longt"));
array[i] = data;
array1[i] = data1;
i++;
} while (cursor.moveToNext());
}
Log.d(TAG, "getData() called");
return convertToJson(array, array1).toString();
}
...
Above is just a snapshot of my index.html file that accesses the Android activity method to showcase the data:
<html>
<head>
<script>
var showData = function() {
var data = android.getData();
data = JSON.parse(data);
window.alert("Hello! Data are: " + data + "; first = " + data[0]);
}
</script>
</head>
<body>
<input type="button" value="Display data" onclick="showData()">
<input type="button" value="Update data" onclick="setData();">
<br/>
<input type="button" value="Done" onclick="android.finish();">
</body>
</html>