使用JSON将多个标记添加到Android Google地图。

时间:2014-04-03 12:32:53

标签: android json google-maps

我想知道如何在Android中为Google地图添加多个标记。我设置的是坐标存储在在线数据库中,然后编码为JSON。然后通过JSON解析器类解析这些JSON坐标以检索信息并存储在arrya中。但是,我似乎无法弄清楚如何迭代存储坐标的JSON数组,以将它们作为标记添加到谷歌地图。

2 个答案:

答案 0 :(得分:2)

试试这个

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();


            }

答案 1 :(得分:0)

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);

}