0

I am calling API then adding JSON data into a arraylist, then calling the arraylist data and placing the Latitude and Longitude onto the map, placing multiple markers. I believe my problem is the getting the string from the ArrayList, but i am not sure how to solve it.

I am calling the API from another Class

private GoogleMap mMap;

ArrayList LatitudeList = new ArrayList();
ArrayList LongitudeList = new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_taxi);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    someMethod();


}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    //Log.d("TEST", "BBB");
    String LatMarkers = LatitudeList.toString();
    String LongMarkers = LongitudeList.toString();

    //Log.d("TEST", "AAA");
        double latitude = Double.parseDouble(LatMarkers);
        double longitude = Double.parseDouble(LongMarkers);
        LatLng location = new LatLng(latitude, longitude);

        //Log.d("TEST", "CCC");
        mMap.addMarker(new MarkerOptions().position(location).title("Camera Location"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(location));

    //Log.d("TEST", "DDD");

}

OkHttpClient client = new OkHttpClient();

void someMethod(){
    try {
        String jsonData = new TAXIAPI(client).execute().get();

        JSONObject object = new JSONObject(jsonData);
        JSONArray RunningArray = object.getJSONArray("value");

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

            JSONObject objectInner = new JSONObject(RunningArray.getString(i));
            String Latitude = objectInner.getString("Latitude");
            String Longitude = objectInner.getString("Longitude");


            LatitudeList.add(Latitude);
            LongitudeList.add(Longitude);

            Log.d("APIActivity", "  " + Latitude + "  " + Longitude );
        }
    } catch (Exception e)
    {
        e.printStackTrace();

    }
}

1 Answer 1

1

you have to call your someMethod() after the map sync

    @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    someMethod();
    }

and use the rest this way within your somemethod() inside for loop :

String Latitude = objectInner.getString("Latitude");
String Longitude = objectInner.getString("Longitude");

double lat =Double.parseDouble(Latidute);
double lng =Double.parseDouble(Longitude);


LatLng location = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(location).title("Camera Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));//this will make camera focus on the last marker location
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.