I need to plot map marker using the values which I get from the JSONArray. I have used the following code.It is working fine when I pass the latitude and longitude values directly but when I pass the values fetched from JSONArray the map isnt getting plotted.
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);
}
Below is my data:
"data": [
{
"id": "43",
"site_no": "361",
"p_id_no": "68-1-361",
"sas_application_no": "95534",
"latitude": "15.824730932928347",
"longitude": "74.50431285009074",
From the comments: With this logic you're assigning the lat
& lon
of the last object in your jsonArray
. It's most likely that by the time you reach your last object, one of your object has caused some JSONException
or your last element has empty or null value. In any case it's being assigned null value. Do check your JSON response fully.
So I created a little demo how you can do it. Read the inline comments to understand:
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);
}
}