微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在android中使用json数组值的android地图标记

如何解决在android中使用json数组值的android地图标记

我需要使用从 JSONArray 获得的值绘制地图标记。我使用了以下代码。当我直接传递纬度和经度值时它工作正常,但是当我传递从 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",

解决方法

来自评论:使用此逻辑,您将分配 lat 中最后一个对象的 lon& jsonArray。最有可能的是,当您到达最后一个对象时,您的一个对象已经导致了一些 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);
    }


}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。