简体   繁体   中英

Android Studio Google Map Change Marker Color

I want to change the marker colors on my map depending on the color inside my database. There's no error so Idk what's wrong with my code. I think it's the for loop but Idk what's the correct logic here.

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

I don't have problems with my markers. Just the for loop. My logcat says there's no value for flowercolor. When I run it, nothing appears on my screen. Hope u can help me.

Simply change data to data2 in your second loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM