在循环中OnMarkerClick

时间:2017-03-31 22:16:27

标签: android json google-maps

在尝试开发具有多个标记的地图应用程序时,允许在单击标记时显示包含信息的弹出窗口,在显示信息时我遇到了这个问题(根据循环,它必须是不同的信息顺序,但它一直要求变量需要是最终的)但我对所有弹出窗口(最后一个记录的值)保持相同的结果,即使我将变量设为全局,抱歉我的英语和请帮助。

@Override
protected void onResume() {
super.onResume();
if(broadcastReceiver == null){
    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

Response.Listener<String> responseListener =new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    if(response== null){

                        Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG);

                    }
                    else {
                        try {
                             jsonObject = new JSONObject(response);
                              jsonArray = jsonObject.getJSONArray("response");
                            int i=0 ;
                            int id,e;
                            Double lt,lg;
                            while (i<jsonArray.length()) {
                                 jo = jsonArray.getJSONObject(i);
                                e=  jo.getInt("etat");

                                id= jo.getInt("driver_id");

                                lt=jo.getDouble("latitude");

                                lg= jo.getDouble("longitude");


                                map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                                    @Override
                                    public boolean onMarkerClick( Marker marker) {
                                        if(marker.getTitle() != null){
                                            return true;
                                        }else{

                                            layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                                            ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.popup,null);

                                            popupWindow = new PopupWindow(container, 500,500,true);
                                            popupWindow.showAtLocation(relativeLayout, Gravity.NO_GRAVITY, 100,100);
                                            TextView tvNom= (TextView) container.findViewById(R.id.tvNom);

                                            tvNom.setText(id +"");

                                            container.setOnTouchListener(new View.OnTouchListener() {
                                                @Override
                                                public boolean onTouch(View view, MotionEvent motionEvent) {
                                                    popupWindow.dismiss();
                                                    return true;
                                                }
                                            });


                                            return false;}
                                    }
                                });
                                if(e==0) {
                                    marker = markerMap.get(d);
                                    if (marker != null) {
                                        marker.remove();
                                        markerMap.remove(d);
                                    } else {
                                        markerMap.remove(d);
                                    }

                                }
                                else{


                                    if (markerMap.containsKey(id)) {
                                        Intent intent= new Intent(MapActivity.this,InfoWindow.class);
                                        intent.putExtra("id",id+"");

                                        // Update the location.
                                        marker = markerMap.get(id);
                                        marker.remove();
                                        markerMap.remove(id); //added
                                        MarkerOptions markerOpt = new MarkerOptions()
                                                .position(new LatLng(lt,lg)).visible(true);
                                        marker = map.addMarker(markerOpt);
                                        markerMap.put(id, marker);


                                    } else {

                                        MarkerOptions markerOpt = new MarkerOptions()
                                                .position(new LatLng(lt, lg)).visible(true);

                                        marker = map.addMarker(markerOpt);
                                        markerMap.put(id, marker);



                                    }   }



                              i++;}


                        }catch (JSONException e) {
                            e.printStackTrace();
                            AlertDialog.Builder nbuilder = new AlertDialog.Builder(MapActivity.this);
                            nbuilder.setMessage("Error")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }

                    }

                }
            };
            RetrieveCoordinates retrieve = new RetrieveCoordinates ( responseListener );
            RequestQueue queue = Volley.newRequestQueue(MapActivity.this);
            queue.add( retrieve);

        }
    };
}
registerReceiver(broadcastReceiver, new IntentFilter("location_update"));

}

1 个答案:

答案 0 :(得分:1)

正如您所指出的,所有侦听器都只添加到地图对象中。 API为每个标记对象附加不同信息提供的是一个名为 tag 的属性。

在标记对象上使用setTag(Object)。

无论你在哪里声明了你的标记,都可以通过它们来附加你想要的标记:

for(int i=0; i < allMarkers.length; i++)
    marker.setTag();

然后分配监听器:

map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                                @Override
                                public boolean onMarkerClick( Marker marker) {
                                    if(marker.getTitle() != null){
                                        return true;
                                    }else{

                                        layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                                        ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.popup,null);

                                        popupWindow = new PopupWindow(container, 500,500,true);
                                        popupWindow.showAtLocation(relativeLayout, Gravity.NO_GRAVITY, 100,100);
                                        TextView tvNom= (TextView) container.findViewById(R.id.tvNom);
                                        //---------retrieve this marker's tag - could be Integer or any other Object.
                                        int id = (Integer)marker.getTag();

                                        tvNom.setText(id +"");

                                        container.setOnTouchListener(new View.OnTouchListener() {
                                            @Override
                                            public boolean onTouch(View view, MotionEvent motionEvent) {
                                                popupWindow.dismiss();
                                                return true;
                                            }
                                        });


                                        return false;}
                                }
                            });