Android Studio Google地图更改标记颜色

时间:2017-11-14 06:40:18

标签: php android sql json google-maps

我想根据数据库中的颜色更改地图上的标记颜色。没有错误,所以Idk我的代码出了什么问题。我认为这是for循环,但Idk这里的逻辑是正确的。

private void AllSpeciesLocation() {
    ArrayList<HashMap<String, String>> location = null;
    String url = "http://192.168.1.4/android_login_api/getLatLong.php";
    try {

        JSONArray data = new JSONArray(getHttpGet(url));

        location = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        for(int i = 0; i < data.length(); i++){
            JSONObject c = data.getJSONObject(i);
            search_species = c.getString("speciesid");
            map = new HashMap<String, String>();
            map.put("locationid", c.getString("locationid"));
            map.put("brgy", c.getString("brgy"));
            map.put("town", c.getString("town"));
            map.put("latitude", c.getString("latitude"));
            map.put("longitude", c.getString("longitude"));
            map.put("speciesid", c.getString("speciesid"));

            String url2 = "http://192.168.1.4/android_login_api/search_species_color.php?species_name=" + search_species;
            try {

                JSONArray data2 = new JSONArray(getHttpGet(url2));

                for(i = 0; i < data2.length(); i++){
                    JSONObject c2 = data.getJSONObject(i);
                    map.put("color", c2.getString("flowercolor"));
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }

            location.add(map);
        }

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

我的标记没有问题。只是for循环。我的logcat说没有flowercolor的价值。当我运行它时,屏幕上没有任何内容。希望你能帮助我。

1 个答案:

答案 0 :(得分:0)

只需在第二个循环中将data更改为data2

        String url2 = "http://192.168.1.4/android_login_api/search_species_color.php?species_name=" + search_species;
        try {

            JSONArray data2 = new JSONArray(getHttpGet(url2));

            for(i = 0; i < data2.length(); i++){
                JSONObject c2 = data2.getJSONObject(i); // This line..
                map.put("color", c2.getString("flowercolor"));
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
        }