dimanche 3 avril 2016

Extract Lat Long from SQlite and display marker on leaflet map

At present i want to display the all lat values on webview by accessing java method from javascript but it is not showing the lat values.

Thank you

this my WebviewActivity.java

 DataHelper myDB = new DataHelper(this);
static final String TAG = "DbWebviewFinal";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data_main);
    myDB.insert(16.5048, 80.6338);
    myDB.insert(16.5024, 80.6432);
    myDB.insert(16.512, 80.6216);
    myDB.insert(16.5124, 80.6219);

    WebView webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    final MyJavaScriptInterface myJavaScriptInterface
            = new MyJavaScriptInterface(this);
    webView.addJavascriptInterface(myJavaScriptInterface, "android");

    webView.loadUrl("file:///android_asset/www/index.html");
}


private class MyJavaScriptInterface {
    Context mContext;

    MyJavaScriptInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public String getData() {
        Cursor cursor = myDB.fetchAllCountries();

        double[] array = new double[cursor.getCount()];
        int i = 0;

        if (cursor.moveToFirst()) {
            do {
                double data = cursor.getDouble(cursor.getColumnIndex("lat"));

                array[i] = data;

                i++;

            } while (cursor.moveToNext());

        }
        Log.d(TAG, "getData() called");


        return a1dToJson(array).toString();
    }
    private String a1dToJson(double[] array) {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < array.length; i++) {
            double d = array[i];
            if (i > 0)
                sb.append(",");
            sb.append(d);
        }
        sb.append("]");
        return sb.toString();
    }

}
}

my index.html which access the android activity method to display the data

<html>
<head>
<script>
    var showData = function() {
        var data = android.getData();
        window.alert("Hello! Data are: " + data + "; first = " + data[0]);
    }
</script>
</head>
<body>
<input type="button" value="Display data" onclick="showData()">
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire