Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Francesco Di Vito
Francesco Di Vito
4,502 Points

Draw google map marker by json

Hi, i'm developing android app with a switch tab that show in one fragment a list of json element, instead into map must show all marker by json. Now the json are load correctly in first fragment instead i have some problem with my code to plotting the json element into map as marker. This is my code:

public class Mappa extends Fragment {
         static final LatLng HAMBURG = new LatLng(53.558, 9.927);
         static final LatLng KIEL = new LatLng(53.551, 9.993);
         private GoogleMap map;
         private static final String LOG_TAG = "ExampleApp";



private static final String SERVICE_URL = "https:xxxxxxxxxxxxxxxxxxxxxxxx";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    drawMarker();


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_mappa, null, false);

    //map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
           // .getMap();
    map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();


    Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
            .title("Hamburg"));
    Marker kiel = map.addMarker(new MarkerOptions()
            .position(KIEL)
            .title("Kiel")
            .snippet("Kiel is cool")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

    // Move the camera instantly to hamburg with a zoom of 15.
    //map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 3));

    // Zoom in, animating the camera.
    //map.animateCamera(CameraUpdateFactory.zoomTo(3), 2000, null);

    //...
    drawMarker();

    return v;



}


public void drawMarker() {
    try {
        JSONObject  jsonRootObject = new JSONObject(SERVICE_URL);
        JSONArray jarray = jsonRootObject.getJSONArray("placemarks");

        for (int i = 0; i < jarray.length(); i++) {
            JSONObject object = jarray.getJSONObject(i);
            String lat = String.valueOf(object.getJSONArray("coordinates").getDouble(1));
            String lon = String.valueOf(object.getJSONArray("coordinates").getDouble(0));
            map.addMarker(new MarkerOptions()
                    .title("pippo")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                    .position(new LatLng(
                            object.getJSONArray("coordinates").getDouble(1),
                            object.getJSONArray("coordinates").getDouble(0)
                    ))

            );
            Log.d("Lat", "valori"+ lat + ";" + lon);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }


}



@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}
}

Why the app doesn't draw marker into map by json? My code is wrang? Any help is great Thanks