android 使用 android 中的 json 数组值绘制标记

android map marker using json array values in android

我需要使用从 JSONArray 获得的值绘制地图标记。我使用了以下 code.It 当我直接传递纬度和经度值时工作正常但是当我传递从 JSONArray 获取的值时地图没有被绘制。

 StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            //converting the string to json array object
                            JSONObject object = new JSONObject(response);
                            if (object.getInt("status") == 200) {
                                JSONArray jsonArray = object.getJSONArray("data");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject pobj = jsonArray.getJSONObject(i);
                                    lat = Double.parseDouble(pobj.getString("latitude"));
                                    lon = Double.parseDouble(pobj.getString("longitude"));                                    pobj.getDouble("latitude")+","+pobj.getDouble("longitude");
                                    Log.i("MAP",""+response);
                                    Log.i("lat-",""+lat);
                                    Log.i("lon-",""+lon);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                                 
    private void DisplayTrack() {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("geo:" + lat  + "," + lon + "?q=" + lat  + "," + lon + ")")); //NOT WORKING
    intent.setData(Uri.parse("geo:" + 15.824730932928347  + "," + 74.50431285009074 + "?q=" + 15.824730932928347  + "," + 74.50431285009074 + ")")); //WORKING
    startActivity(intent);
}

以下是我的数据:

"data": [
    {
        "id": "43",
        "site_no": "361",
        "p_id_no": "68-1-361",
        "sas_application_no": "95534",
        "latitude": "15.824730932928347",
        "longitude": "74.50431285009074",

来自评论:使用此逻辑,您将分配 jsonArray 中最后一个对象的 lat& lon。最有可能的是,当您到达最后一个对象时,您的一个对象已经导致了一些 JSONException 或者您的最后一个元素具有空值或 null 值。在任何情况下,它都被分配了空值。请务必全面检查您的 JSON 回复。

所以我创建了一个小演示,您可以如何操作。阅读内联评论以了解:

public class MainActivity extends AppCompatActivity {

    //declare the global variable, initially they are null
    private Double lat, lon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // prepare request
        StringRequest stringRequest = new StringRequest(Request.Method.POST, "url",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            //converting the string to json array object
                            JSONObject object = new JSONObject(response);
                            //check
                            if (object.getInt("status") == 200) {
                                JSONArray jsonArray = object.getJSONArray("data");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    //keep getting the new objects
                                    JSONObject pobj = jsonArray.getJSONObject(i);
                                    //assign the last object lat/lon
                                    lat = Double.parseDouble(pobj.getString("latitude"));
                                    lon = Double.parseDouble(pobj.getString("longitude"));
                                }
                                //if they are not null then call the activity
                                if (lat != null && lon != null) {
                                    DisplayTrack(lat, lon);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        // call your request like you do
    }

    //declared the function out of the Network call scope
    private void DisplayTrack(Double lat, Double lon) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("geo:" + lat + "," + lon + "?q=" + lat + "," + lon + ")")); //NOT WORKING
        startActivity(intent);
    }


}