ScroogeMcDuck
ScroogeMcDuck

Reputation: 1

Adding Multiple Markers to Android Google maps with JSON.

I was wondering how you add multiple markers to a Google Map in Android.The set up I have is the coordinates are stored in an online database and then encoded into JSON. These JSON coordinates are then parsed through a JSON parser class to retrieve the information and stored in an arrya. However I can't seem how to figure out how to iterate through the JSON Array where the coordinates are stored to add them as markers to a google map.

Upvotes: 0

Views: 1906

Answers (2)

MiguelAngel_LV
MiguelAngel_LV

Reputation: 1258

A for and addMaker

for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject j = jsonarray.optJSONObject(i);

    MarkerOptions m = new MarkerOptions()
      .title(j.optString("title"))
      .position(new LatLng(j.optDouble("lat"), j.optDouble("lng")))

   gmap.addMarker(m);

}

Upvotes: 0

jack
jack

Reputation: 348

try this

if (result != null) {

                try {
                    JSONArray jArray = new JSONArray(result);
                    Log.i("JSON ARRAY","" + jArray);

                    for(int i=0;i<jArray.length();i++){



                        JSONObject tableData = jArray.getJSONObject(i);

                        lot_id = tableData.getString("id");


                        lot_number = tableData.getString("lot_number");


                        coordinate_late = tableData.getString("coordinate_late");


                        coordinate_long = tableData.getString("coordinate_long");


                            latitude_lot_numer = Double.parseDouble(coordinate_late);
                            longitude_lot_number = Double.parseDouble(coordinate_long);
                             // create marker
                                MarkerOptions client_marker = new MarkerOptions().position(new LatLng(latitude_lot_numer, longitude_lot_number));
                                client_marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin));
                                // adding marker
                                mMap.addMarker(client_marker);

                        arraylist_of_lot_number.add(lot_number);
                        Log.i("ArrayList Async in loop",""+ arraylist_of_lot_number);
                        lot_id_arraylist.add(lot_id);


                        if(arraylist_of_lot_number.isEmpty()){
                            Toast.makeText(getApplicationContext(), "Empty List for Unpaid Lot", Toast.LENGTH_LONG).show();
                        }

} catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();


            }

Upvotes: 2

Related Questions