简体   繁体   中英

How to Foreach the marker in Maps, using retrofit2?

I have problem in foreach the marker in Maps Android Google Maps API

I've tried using ArrayList foreach, but the marker on the map still doesn't appear, maybe I have an error in my syntax

private void Koordinat() {

        Call<List<LatlongModel>> sektorCall = latlongInterface.GetLatlong();
        sektorCall.enqueue(new Callback<List<LatlongModel>>() {

            @Override
            public void onResponse(Call<List<LatlongModel>> call, Response<List<LatlongModel>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listLatlong = response.body();
                    ArrayList<Double> itemLatlong = new ArrayList<Double>();
                    for (int x = 0; x < response.body().size(); x++) {

                        Double latitude = response.body().get(x).getLatitude();
                        Double longitude = response.body().get(x).getLongitude();

                        itemLatlong.add(latitude);
                        itemLatlong.add(longitude);

                        latLongA = new LatLng(longitude, latitude);


                    }

                    mMap.addMarker(new MarkerOptions().position(latLongA)
                            .title("Citarum")
                            .snippet("Titik A")
                            .rotation((float) 3.5)
                            .icon(bitmapDescriptorFromVector(getApplicationContext(), R.drawable.ic_action_hijau)));


                } else {
                    Toast.makeText(getBaseContext(), "response message"+ response.body().toString(), Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<List<LatlongModel>> call, Throwable t) {

                t.printStackTrace();
            }
        });

    }

Markers come from the following JSON response:

{"id": 1, "titk": "Titik A1", "lat": -6.9155332, "long": 107.4724021, "idSector": 1}

{"id": "1," titk":"Titik A2"," lat ": -6.9153333," long ": 107.4725922," idSector ": 1}

My expectations are for markers to appear on maps from the lat and long JSON responses above

Assuming nMap is already initialized, I believe your mMap.addMarker should be inside the loop instead of being outside. Can you check that?

Adding the markers to your map should be done inside the for loop not outside. Your code should look something like below.

Note: I changed the for loop into a foreach loop to make things look cleaner.

private void Koordinat() {

        Call<List<LatlongModel>> sektorCall = latlongInterface.GetLatlong();
        sektorCall.enqueue(new Callback<List<LatlongModel>>() {

            @Override
            public void onResponse(Call<List<LatlongModel>> call, Response<List<LatlongModel>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listLatlong = response.body();
                    for (LatlongModel latLongModel : response.body()) {

                        LatLng latLongA = new LatLng(latLongModel.getLatitude(), latLongModel.getLongitude());

                        mMap.addMarker(new MarkerOptions().position(latLongA)
                            .title("Citarum")
                            .snippet("Titik A")
                            .rotation((float) 3.5)
                            .icon(bitmapDescriptorFromVector(getApplicationContext(), R.drawable.ic_action_hijau)));

                    }




                } else {
                    Toast.makeText(getBaseContext(), "response message"+ response.body().toString(), Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<List<LatlongModel>> call, Throwable t) {

                t.printStackTrace();
            }
        });

    }

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