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&jsonArray中最后一个对象的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);
}

}

相关内容

  • 没有找到相关文章

最新更新