首页 > 解决方案 > 如何使用retrofit2在地图中搜索标记?

问题描述

我在 Maps Android Google Maps API 中的 foreach 标记有问题

我试过使用 ArrayList foreach,但地图上的标记仍然没有出现,也许我的语法有错误

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

    }

标记来自以下 JSON 响应:

{“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}

我的期望是标记出现在上面的 lat 和 long JSON 响应的地图上

标签: javaandroid

解决方案


假设 nMap 已经初始化,我相信你的 mMap.addMarker 应该在循环内而不是在循环外。你能检查一下吗?


推荐阅读