Eudes
Eudes

Reputation: 13

Create Marker with lat and long from JSON file inside Assets folder

I created an android application and want to add Markers to my map with lat and long from JSON file inside Assets folder. I dont know what is the error in my code, my markers dont show up.

this is my Json file:

{
 "data" : [
{
"title": "Loja Coqueiros",
"lat": -8.8123083,
"lng": 13.2249500
},

 {
"title": "Loja Amilca Cabral",
"lat": -8.8265861,
"lng": 13.2274667
},

 {
"title": "Loja samba",
"lat":-8.8328611,
"lng": 13.2182861
}
]


 }

this is how i fetch the JSON file:

public String getJSONFromAssets() {
        String json = null;
        try {
            InputStream inputData = getAssets().open("locations.json");
            int size = inputData.available();
            byte[] buffer = new byte[size];
            inputData.read(buffer);
            inputData.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;

    }

I have added to my Array:

JSONObject obj = new JSONObject(getJSONFromAssets());
    JSONArray arr = obj.getJSONArray("data");
    if (arr != null)  
       for (int i=0;i<arr.length();i++)
           locations.add(arr.get(i).toString()); 

And i create my marker :

for (int i=0;i< locations.size();i++) {

            try {
                map.addMarker(new MarkerOptions()
                        .position(new LatLng(obj.getJSONArray("lat").getDouble(0),
                                             obj.getJSONArray("lng").getDouble(0)))

                        .title((obj.getString("title")))
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map_marker)));
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }

Upvotes: 0

Views: 152

Answers (2)

Giteeka Sawlani
Giteeka Sawlani

Reputation: 465

As User dolphinziyo said The problem is in obj.getJSONArray("lat").getDouble(0), you are accessing it in a wrong way.
Please replace your for loop with below for loop :

for (int i=0;i< locations.size();i++) {
         JSONObject locationObj = new JSONObject(locations.get(i));
        try {
            map.addMarker(new MarkerOptions()
                    .position(new LatLng(locationObj.getDouble("lat"),
                            locationObj.getDouble("lan"))
                    .title((locationObj.getString("title")))
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map_marker)));
        } catch (JSONException e) {
            e.printStackTrace();
    }

    }

Upvotes: 2

Alberto M&#233;ndez
Alberto M&#233;ndez

Reputation: 1064

The problem is in obj.getJSONArray("lat").getDouble(0), you are accessing it in a wrong way. Get it from locations and parse it to double or if you want to get it from your json you must use arr:

arr.getJSONObject(0).getDouble("lat");

If you just want to get it right from the full JSON you must do:

obj.getJSONArray("data").getJSONObject(0).getDouble("lat");

Upvotes: 0

Related Questions