Jade
Jade

Reputation: 83

How to display my arraylist with coordinates on google maps?

I want to display my arraylist filled with coordinates which I have in my MainActivity on google maps in my google maps activity. How can I do this on the easiest way? My for loop to get the JSON Data:

for (int j = 0; j <= 10; j++) {
JSONObject marker = placemarks.getJSONObject(j);
JSONArray coordinates = marker.optJSONArray("coordinates");
double lat = coordinates.optDouble(0);
double lng = coordinates.optDouble(1);
latlongList.add(new LatLng(lat, lng));
}

My current google maps activity:

public class CarLocation extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car2_go_location);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {

        MainActivity mainActivity = new MainActivity();

        mMap = googleMap;
        // Add a marker in Hamburg and move the camera
        LatLng hamburg = new LatLng(53.551086, 9.993682);
        mMap.addMarker(new MarkerOptions().position(hamburg).title("Marker in Hamburg"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(hamburg));

    }
}

Upvotes: 0

Views: 1291

Answers (2)

blackkara
blackkara

Reputation: 5052

I did not test this code, but to make sense how to do it, you can follow.

I mentioned in my comment above, instead of sending whole list between activites (in your case from MainActivity to CarLocation activity), it's better to wrap whole process (fetching json and displaying markers) in a method and use.

DisplayMarkersTask asynctask fetches and displays markers.

public class CarLocation extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private List<Marker> markers = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car2_go_location);

        SupportMapFragment mapFragment = 
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        mapFragment.getMapAsync(this);
    }

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

        Send parameter if needed
        String param = .... 
        new DisplayMarkersTask(param).execute();
    }

    private static class DisplayMarkersTask extends AsyncTask<String, Void, ArrayList<LatLng>> {

        private WeakReference<CarLocation> mActivityRef;
        public DisplayMarkersTask(CarLocation activity){
            mActivityRef = new WeakReference<>(activity);
        }

        @Override
        protected ArrayList<LatLng> doInBackground(String... params) {
            Take parameter if needed
            String param = params[0];
            ....
            Fetch your json here

            ArrayList<LatLng> latlongList = new ArrayList<>();
            for (int j = 0; j <= 10; j++) {
                JSONObject marker = placemarks.getJSONObject(j);
                JSONArray coordinates = marker.optJSONArray("coordinates");
                double lat = coordinates.optDouble(0);
                double lng = coordinates.optDouble(1);
                latlongList.add(new LatLng(lat, lng));
            }
            return latlongList;
        }

        @Override
        protected void onPostExecute(ArrayList<LatLng> list) {
            CarLocation activity = mActivityRef.get();
            if(activity == null){
                return;
            }

            for (int j = 0; j <= list.size(); j++) {
                LatLng position = list.get(j);
                Marker marker = activity.mMap.addMarker(
                    new MarkerOptions().position(position));

                activity.markers.add(marker);
            }
        }
    }
}

Upvotes: 2

Related Questions