javascript - 从 SQlite 中提取 Lat Long 并显示在 webview 上

标签 javascript java android sqlite leaflet

目前我想通过从 javascript 访问 java 方法来在 webview 上显示所有经纬度值,但它没有显示纬度经度值。

请帮忙解决

谢谢

这是我的 WebviewActivity.java

static final String TAG = "JavascriptDataDemo";

//double[] data = new double[] {42.6, 24, 17, 15.4};
DataHelper myDB=new DataHelper(this);

/** This passes our data out to the JS */
@JavascriptInterface
public String getData() {
    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 a1dToJson(array,array1).toString();
}

/** Allow the JavaScript to pass some data in to us.
@JavascriptInterface
public void setData(String newData) throws JSONException {
    Log.d(TAG, "MainActivity.setData()");
    JSONArray streamer = new JSONArray(newData);
    data = new double[streamer.length()];
    for (int i = 0; i < streamer.length(); i++) {
        Double n = streamer.getDouble(i);
        data[i] = n;
    }
}*/

private Activity activity;

public Context getActivity() {
    return activity;
}

public void setActivity(Activity app) {
    this.activity = app;
}

@JavascriptInterface
public void finish() {
    Log.d(TAG, "ArrayApplication.finish()");
    activity.finish();
}

/** Sorry for not using the standard org.json.JSONArray but even in Android 4.2 it lacks
 * the JSONArray(Object[]) constructor, making it too painful to use.
 */
private String a1dToJson(double[] data,double[] data1) {
    StringBuffer sb = new StringBuffer();
    sb.append("[");
    for (int i = 0,j=0; (i < data.length) && (j<data1.length); i++,j++) {
        double d = data[i];
        double s=data1[j];
        if (i > 0 && j>0) {
            sb.append("[");
            sb.append(d);
            sb.append(",");
            sb.append(s);
            sb.append("]");
        }
    }
    sb.append("]");
    return sb.toString();
}
}

我的index.html,它访问android Activity 方法来显示数据

<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>

最佳答案

终于得到答案了

DataHelper myDB=new DataHelper(this);


//double[] data = new double[] {42.6, 24, 17, 15.4};

/** This passes our data out to the JS */
@JavascriptInterface
public String getData() {

    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[] arr=new double[cursor.getCount()];
    int i=0;

    if(cursor.moveToFirst())
    {
        do {
            double data=cursor.getDouble(cursor.getColumnIndex("lat"));
           arr[i]=data;
            i++;

        }while (cursor.moveToNext());

    }




    Log.d(TAG, "getData() called");
    return a1dToJson(arr).toString();
}


@JavascriptInterface
public String getLong() {

    Cursor cursor1=myDB.fetchAllCountries();
    double[] arr1=new double[cursor1.getCount()];
    int i=0;

    if(cursor1.moveToFirst())
    {
        do {
            double data1=cursor1.getDouble(cursor1.getColumnIndex("longt"));
            arr1[i]=data1;
            i++;

        }while (cursor1.moveToNext());

    }




    Log.d(TAG, "getData() called");
    return a1dToJson(arr1).toString();
}


private Activity activity;

public Context getActivity() {
    return activity;
}

public void setActivity(Activity app) {
    this.activity = app;
}

@JavascriptInterface
public void finish() {
    Log.d(TAG, "ArrayApplication.finish()");
    activity.finish();
}

/** Sorry for not using the standard org.json.JSONArray but even in Android 4.2 it lacks
 * the JSONArray(Object[]) constructor, making it too painful to use.
 */
private String a1dToJson(double[] data) {
    StringBuffer sb = new StringBuffer();
    sb.append("[");
    for (int i = 0; i < data.length; i++) {
        double d = data[i];
        if (i > 0)
            sb.append(",");
        sb.append(d);
    }
    sb.append("]");
    return sb.toString();
}

javascript 文件是

<html>
<head>

<script>
    var showData = function() {
        var data = android.getData();
        data=JSON.parse(data);
         var data1 = android.getLong();
        data1=JSON.parse(data1);
        window.alert("Hello! Data are: " + data );
        window.alert("Hello! Data are: " + data1 );
    }

 </script>

   </head>

 <body>

    <input type="button" value="Display data" onclick="showData()">


    <input type="button" value="Done" onclick="android.finish();">

     </body>
      </html>

关于javascript - 从 SQlite 中提取 Lat Long 并显示在 webview 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36395856/

相关文章:

java - 致命异常 : main java. lang.NoClassDefFoundError : rx. plugins.RxJavaHooks

android - 具有不同高度项目的两列 ListView

javascript - 从更高范围内的 Promise 获得返回值

java - 如何解决拍摄屏幕截图并将其导出到 Listener 类中的范围报告以进行测试的问题?

Java助手,java

java - JTextField 文本更改的监听器

android - 如何将嵌套哈希表序列化/反序列化为 JSON?

javascript - MongoDB/Node.JS : Inserting documents in a loop - variables not updating?

javascript - 如何将 Three/js 相机控件从第一人称切换到轨道并返回

javascript - React JS音频不播放